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. Link to comment https://forums.phpfreaks.com/topic/284905-regex-for-letters-hyphens-space-and-periods-only-no-numbers/ Share on other sites More sharing options...
.josh Posted December 23, 2013 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); Link to comment https://forums.phpfreaks.com/topic/284905-regex-for-letters-hyphens-space-and-periods-only-no-numbers/#findComment-1463011 Share on other sites More sharing options...
AJinNYC Posted December 23, 2013 Author Share Posted December 23, 2013 Excellent thanks. Link to comment https://forums.phpfreaks.com/topic/284905-regex-for-letters-hyphens-space-and-periods-only-no-numbers/#findComment-1463024 Share on other sites More sharing options...
Psycho 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. 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); } Link to comment https://forums.phpfreaks.com/topic/284905-regex-for-letters-hyphens-space-and-periods-only-no-numbers/#findComment-1463025 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.