CincoPistolero Posted November 3, 2009 Share Posted November 3, 2009 I want a field to be letters only "-" and "_". No special characters. No Numbers. I have the javascript form validation working, however, I also want PHP form validation set. Below is what I have coded for checking to make sure the field only contains the characters mentioned above. Now, when I enter just letters in the field, it gives me the error I have coded. Letters should work, but are failing. // Check for numbers in first name if (!eregi('^[a-zA-Z_-]$',$_POST['fname'])) { die('<div id=error><table cellpadding="0" cellspacing="0" border="0" align="center" bgcolor="#ffffff"> <tr> <td><br><br>No numbers or special characters allowed in first name.<br><a href="javascript:history.back()">Back</a><br><br></td> </tr> </table></div>'); } Quote Link to comment https://forums.phpfreaks.com/topic/180173-solved-form-validation/ Share on other sites More sharing options...
.josh Posted November 3, 2009 Share Posted November 3, 2009 a character class only matches against 1 character, and you have start of string and end of string anchors wrapped around it, so it will fail if you have more than 1 character entered. You need to add a quantifier to it. Also, eregi is deprecated, use preg_match instead. if (!preg_match('^[a-zA-Z_-]+$',$_POST['fname'])) { the + is the quantifier. It means 1 or more of what's inside that character class. If you want it to be a specific amount, like only 5-10 characters, do this instead: if (!preg_match('^[a-zA-Z_-]{5,10}$',$_POST['fname'])) { Quote Link to comment https://forums.phpfreaks.com/topic/180173-solved-form-validation/#findComment-950520 Share on other sites More sharing options...
CincoPistolero Posted November 3, 2009 Author Share Posted November 3, 2009 I now have it like this if (!preg_match('^[a-zA-Z_-]+$',$_POST['fname'])) { it still fails with normal text, and I get this error Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in /home/pastwork/public_html/empl/empInfo.php on line 302 Quote Link to comment https://forums.phpfreaks.com/topic/180173-solved-form-validation/#findComment-950525 Share on other sites More sharing options...
.josh Posted November 3, 2009 Share Posted November 3, 2009 ah sorry, forgot to put the delimiters in. if (!preg_match('~^[a-zA-Z_-]+$~',$_POST['fname'])) { Quote Link to comment https://forums.phpfreaks.com/topic/180173-solved-form-validation/#findComment-950530 Share on other sites More sharing options...
CincoPistolero Posted November 3, 2009 Author Share Posted November 3, 2009 What exactly do the delimeters do? Quote Link to comment https://forums.phpfreaks.com/topic/180173-solved-form-validation/#findComment-950539 Share on other sites More sharing options...
.josh Posted November 3, 2009 Share Posted November 3, 2009 They delimit. http://dictionary.reference.com/browse/delimiter Quote Link to comment https://forums.phpfreaks.com/topic/180173-solved-form-validation/#findComment-950543 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.