AJinNYC Posted December 23, 2013 Share Posted December 23, 2013 Need some help to get the right RegEx pattern for a preg_match check on names. It must match letters, hyphens, spaces, and periods only. No numbers or any other symbols. /*Verify that Names are in the proper format*/ function name_verify($name){ return preg_match("/^\b[\p{L}](?:[\p{L}. -]+[\p{L}])?\b$/u", $name); } This works except when there is a period in the value. Need periods for initials. Quote Link to comment Share on other sites More sharing options...
Solution .josh Posted December 23, 2013 Solution Share Posted December 23, 2013 okay well the way your regex is currently written, you expect a single unicode letter, followed by 1 or more unicode letters, dots, spaces or hyphens, and then followed by 1 unicode letter. So IOW currently it won't allow for the string to start or end with a period (or any of your other non-letter chars). If you just want to match for those things regardless of position, do this: return preg_match("/^[\p{L}. -]+$/u", $name); Quote Link to comment Share on other sites More sharing options...
AJinNYC Posted December 23, 2013 Author Share Posted December 23, 2013 Excellent thanks. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 23, 2013 Share Posted December 23, 2013 (edited) Need some help to get the right RegEx pattern for a preg_match check on names. It must match letters, hyphens, spaces, and periods only. No numbers or any other symbols. Good thing my last name isn't "O'Reilly" - else you would tell me my last name is invalid. EDIT: when validating if something like this, I find it easier to check for any that are NOT within the acceptable list rather than checking that all are valid. I would use this: function validName($name) { return !preg_match("#[^\p{L}. -]#u", $name); } Edited December 23, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.