php - what is the proper way of using an if condition with RegEx -


i have been trying validate form input first , last name using regex in php. need regex check make sure there no numbers. have right now:

if (preg_match('/\a\b[^0-9]*\w[^0-9]*\b\z/sm', $name)) { # successful match $nameerror = ""; echo $name; } else { # match attempt failed\ $nameerror = "no numbers"; } 

the $name variable holds first , last name. have been trying make work , have not been able input match regex. using correctly or need input in way. thank help

if name surename , first name should use condition depending on country example in poland be

preg_match('/[a-z]+ [a-z]+/i',$name);  

it means names contains 2 part alphabetic space separating them good. if want first letter of name upper should change to

preg_match('/[a-z][a-z]+ [a-z][a-z]+/',$name);  

preg_match returns true if $name validated regular expression provide in first argument.

so usage of function okay, should check expression.

http://pl1.php.net/preg_match

preg_match() returns 1 if pattern matches given subject, 0 if not, or false if error occurred.

you can check regex on online checker example

http://www.solmetra.com/scripts/regex/


Comments