CincoPistolero Posted November 4, 2009 Share Posted November 4, 2009 I have a field that is not required. I only want the regex to validate if something is entered. With what I have below it tries to validated the blank data. Is there away to make it ignore (i'm not sure what to call it) white space, empty space, non entered data. // Check for wrong characters in phone2 if(!preg_match('~^[0-9.-]+$~',$_POST['phone2'])) { die('<div id=error><table cellpadding="0" cellspacing="0" border="0" align="center" bgcolor="#ffffff"> <tr> <td><br><br>Only numbers in second phone number.<br><a href="javascript:history.back()">Back</a><br><br></td> </tr> </table></div>'); } Quote Link to comment https://forums.phpfreaks.com/topic/180240-regex-ugh/ Share on other sites More sharing options...
cags Posted November 4, 2009 Share Posted November 4, 2009 There is, but it would be far easier to just use the empty function. If(!empty($_POST['phone2'])) { if(!preg_match('~^[0-9.-]+$~',$_POST['phone2'])) { // blah } } Quote Link to comment https://forums.phpfreaks.com/topic/180240-regex-ugh/#findComment-950911 Share on other sites More sharing options...
Daniel0 Posted November 5, 2009 Share Posted November 5, 2009 That's not a very good regular expression for matching phone numbers though. -.........234-.-.23.-.-23-.4-.2-.34 would be a valid phone number according to that regex. Quote Link to comment https://forums.phpfreaks.com/topic/180240-regex-ugh/#findComment-951674 Share on other sites More sharing options...
.josh Posted November 6, 2009 Share Posted November 6, 2009 // assumed american area code + number ... if (strlen(preg_replace('~[^0-9]~','',$_POST['phone2'])) != 10) { // number not valid } Quote Link to comment https://forums.phpfreaks.com/topic/180240-regex-ugh/#findComment-952203 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.