Genesis730 Posted August 17, 2011 Share Posted August 17, 2011 So I'm trying to get this regex to work properly, I'm wanting to only accept uppercase letters, lowercase letters, one - (hyphen) and one ' (apostrophe). Everything seems to work except when i put a ' in the input, it fails. $regex = "/[^-'a-z]/i"; if (preg_match($regex,$lastname) || substr_count($lastname, "'") > 1 || substr_count($lastname, "-") > 1) { $errormessage = 'Last Name Invalid'; $error += 1; } Any help would be appreciated Link to comment https://forums.phpfreaks.com/topic/244981-regex-expression-not-working/ Share on other sites More sharing options...
codefossa Posted August 17, 2011 Share Posted August 17, 2011 <?php $regex = '/[^-\'a-z]/i'; if (preg_match($regex, $lastname) || substr_count($lastname, '\'') > 1 || substr_count($lastname, '-') > 1) { $errormessage = 'Last Name Invalid'; $error += 1; } ?> Link to comment https://forums.phpfreaks.com/topic/244981-regex-expression-not-working/#findComment-1258430 Share on other sites More sharing options...
Genesis730 Posted August 17, 2011 Author Share Posted August 17, 2011 Still fails when i use your suggestion and try to escape the ' Link to comment https://forums.phpfreaks.com/topic/244981-regex-expression-not-working/#findComment-1258431 Share on other sites More sharing options...
codefossa Posted August 17, 2011 Share Posted August 17, 2011 It's workin' fine for me. <?php $lastname = "MichA-'el"; // Works // $lastname = "MichA-'el'"; // Errors out with the second apostrophe $regex = '/[^-\'a-z]/i'; if (preg_match($regex, $lastname) || substr_count($lastname, '\'') > 1 || substr_count($lastname, '-') > 1) { $errormessage = 'Last Name Invalid'; $error += 1; } ?> Link to comment https://forums.phpfreaks.com/topic/244981-regex-expression-not-working/#findComment-1258432 Share on other sites More sharing options...
Genesis730 Posted August 17, 2011 Author Share Posted August 17, 2011 I have copied and pasted it exactly, and MichA-'el fails on mine being that I'm using " in the regex i didn't even think that i had to escape a ' but even doing so I get the same result, so could there possibly be a setting that's altering something and causing my issue?? Link to comment https://forums.phpfreaks.com/topic/244981-regex-expression-not-working/#findComment-1258446 Share on other sites More sharing options...
codefossa Posted August 17, 2011 Share Posted August 17, 2011 Without editing your code, it works fine for me. <?php $lastname = "miCHa-'el"; $regex = "/[^-'a-z]/i"; if (preg_match($regex,$lastname) || substr_count($lastname, "'") > 1 || substr_count($lastname, "-") > 1) { $errormessage = 'Last Name Invalid'; $error += 1; } ?> Link to comment https://forums.phpfreaks.com/topic/244981-regex-expression-not-working/#findComment-1258478 Share on other sites More sharing options...
Genesis730 Posted August 17, 2011 Author Share Posted August 17, 2011 Just realized i was using MRES so yes, that was the solution : Problem SOLVED! Link to comment https://forums.phpfreaks.com/topic/244981-regex-expression-not-working/#findComment-1258757 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.