PHP regex for name verification -
so have assignment. have validate name input can contain big , small letters, apostrophe, spaces or comas. i've come validation:
$name = $_post['your_name']; $regex_name = '/([a-z]|[a-z])([a-z][a-z\s])+/'; if (!preg_match($regex_name, $name)) { echo '<span style="color:red">please input valid name!</span><br />'; }
but doesn't seem work fine , don't know why.
i've read regex , rules don't i'm doing wrong. i've seen examples here on stackoverflow it's still not clear me.
i think should validate @ least letters gives false simple input 'error'.
please help!
"doesn't work fine" not helpful description. i'd guess problem aren't anchoring query start of string (^
) , end of string ($
). means you're matching subset of characters anywhere in string.
also, didn't in question, in code looks want allow letters first character. should trick you:
$name = $_post['your_name']; $regex_name = "/^[a-z][a-z,' ]+$/i"; if (!preg_match($regex_name, $name)) { echo '<span style="color:red">please input valid name!</span><br />'; }
i've used i
modifier @ end of expression make search case-insensitive.
Comments
Post a Comment