开发者

Validating telephone, email adress and Name inputs with PHP; Need regexp if there are any available

I have a php script which I need to validate several inputs with.

Is there any reliable and very good regular expression to check against when it comes to telephone nr, name and email adress validation?

Could somebody please supply these as I am very novice in regexp?

What I want is for example:

Telephone Nr: all number allowed, must be atleast 6 numbers, max 12 numbers, '+' sign allowed, space allowed, '-' sign allowed, as well as other things I haven't thought about yet.

Name: No numbers allowed, only characters in both lower and uppercase. Also the three swedish chars 'Å, Ä, Ö' in both lower and uppercase, also space, '-' sign allowed, and all others I havent thought about.

Email: Email adress is pretty standard over the world, so I don't know exactly what to ask for here, but you probably know what I w开发者_StackOverflow中文版ant.

Thanks for all help


As Andrew White said, emails shouldn't be validated [only] by regex, but you can check out this one:

'/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i'

it's closest to the email address spec I've ever found (no tests have been found which it fails)... can't remember where it's from, will edit my answer as soon as I find it again

[EDIT]

Found it, definitely worth a read: http://fightingforalostcause.net/misc/2006/compare-email-regex.php

[EDIT]

This should do for the phone numbers:

<?php

function is_valid_phonenumber( $subject ) {

  // strip all valid chars
  $stripped = preg_replace( '{[0-9 +-]}', '', $subject );

  // check if there are remains, if yes: fail
  if( !empty( $stripped ) )
    return false;

  // get digit count by replacing everything except digits with nothing
  $digits = strlen( preg_replace( '{[^0-9]}', '', $subject ) );

  // invalid if less than 6 or more than 12 in length
  if( $digits < 6 || $digits > 12 )
    return false;

  // if nothing fails before this, we're good to go
  return true;

}

?>

Similar can be done for the names, but don't forget the case-insensetive flag (i.e. '{pattern}i', there are also some good regex cheat sheets out there, for example this one from addedbytes.com: http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/


Validating telephone numbers with a regex is one thing but e-mails should not be validated by regex. I guess if you just wanted a very basic this-is-an-email regex you could use...

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

For telephone I think the following is good for US/Canada...

^[01]?[- .]?\(?[2-9]\d{2}\)?[- .]?\d{3}[- .]?\d{4}$

For names, good luck since names can be just about anything including numbers in some odd cases (Sr. Fracis John 2nd vs II). That all said I recommend you look into library specific validators for each type if it really matters but my PHP is a bit rusty so I don't have a recommendation there.


Have a read of: http://www.regular-expressions.info/email.html which discusses validating email addresses with Regex

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜