mottwsc Posted March 11, 2011 Share Posted March 11, 2011 This should be simple, but it doesn't seem to work. This preg_match should not be allowing other characters like # % and others, but it does. In other words, it does not hit the error message section if a pound sign or percent sign is entered in the 'answer' field on the form. I'm trying to only allow the characters that are listed: letters, numbers, single quote, exclamation, period, comma, space, dash and question mark. Can anyone see why this wouldn't be working? Thanks! $answerClean = trim($_POST['answer']); if( !preg_match("/^[A-Za-z0-9'!., -?]{2,150}$/", $answerClean) ) { $message = $message."You have not provided a valid answer for the question."; } Quote Link to comment https://forums.phpfreaks.com/topic/230383-preg_match-characters-slipping-through/ Share on other sites More sharing options...
silkfire Posted March 12, 2011 Share Posted March 12, 2011 It's because your regex is flawed. I would suggest to change it to the following. You need to escape all the special regex characters that are reserved. $answerClean = trim($_POST['answer']); if(!preg_match('#^[A-Za-z0-9\'!\., \-?]{2,150}$#', $answerClean)) $message .= 'You have not provided a valid answer for the question.'; Quote Link to comment https://forums.phpfreaks.com/topic/230383-preg_match-characters-slipping-through/#findComment-1186454 Share on other sites More sharing options...
mottwsc Posted March 12, 2011 Author Share Posted March 12, 2011 This works - thanks silkfire. I understand the escaping of special characters, but why did you wrap everything in hash signs (#)? Quote Link to comment https://forums.phpfreaks.com/topic/230383-preg_match-characters-slipping-through/#findComment-1186466 Share on other sites More sharing options...
Psycho Posted March 12, 2011 Share Posted March 12, 2011 I understand the escaping of special characters, but why did you wrap everything in hash signs (#)? That is probably his preference (it is mine as well). You can use just about any character to deliniate your regex expression. But, the problem with your original pattern was the period which as a wildcard will match any character. That is why it needed to be escaped. Quote Link to comment https://forums.phpfreaks.com/topic/230383-preg_match-characters-slipping-through/#findComment-1186480 Share on other sites More sharing options...
silkfire Posted March 12, 2011 Share Posted March 12, 2011 Glad it worked, mate Ye and the unescaped hyphen which caused a bug in the pattern. Quote Link to comment https://forums.phpfreaks.com/topic/230383-preg_match-characters-slipping-through/#findComment-1186553 Share on other sites More sharing options...
mottwsc Posted March 12, 2011 Author Share Posted March 12, 2011 OK - thanks to both of you Quote Link to comment https://forums.phpfreaks.com/topic/230383-preg_match-characters-slipping-through/#findComment-1186556 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.