Good afternoon!<br /><br />I am writing a function to try and validate a phone number field using a preg_match function. I am having a hard time understanding how it works but this is what I've got so far. It should accept an entry in this format: xxx-xxx-xxxx and if it isn't, then it should return false.<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function checkPhone($element){<br />Â Â Â Â $phoneno = trim($element);<br />Â Â Â Â return !preg_match("(^(.*)[0-9]{3})(.*)([0-9]{3})(.*)([0-9]{4}$)", $phoneno);<br />}<!--c2--></div><!--ec2--><br /><br />I call this function in my code like this:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if (!@checkPhone($_POST['phoneno'])){ <br />Â Â Â Â $msg=$msg."Please enter a valid phone number.<br>";<br />Â Â Â Â $flag = "NOTOK";<br />}else{<br />Â Â Â Â ....blah blah blah this code is if the phone number checks out ok<br />}<!--c2--></div><!--ec2--><br /><br />But when I run the function in my debugger, it shows me this warning:<br />Debug Warning: validation.php line 29 - preg_match(): Unknown modifier '('<br /><br /><br />Any ideas?<br />Thanks in advance.<!--content-->
Don't know about your code but here is an example I found that does what you want.<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->A very simple Phone number validation function.<br />Returns the Phone number if the number is in the xxx-xxx-xxxx format. x being 0-9.<br />Returns false if missing digits or improper characters are included.<br /><br /><?<br />function VALIDATE_USPHONE($phonenumber)<br />{<br />if ( (preg_match("/^[0-9]{3,3}[-]{1,1}[0-9]{3,3}[-]{1,1}<br />Â Â Â Â [0-9]{4,4}$/", $phonenumber) ) == TRUE ) {<br />Â Â return $phonenumber;<br /> } else {<br />Â Â return false;<br />Â Â }<br />}<br /><br />?><!--c2--></div><!--ec2--><!--content-->
Bruce-<br />First off, thanks for the code.<br /><br />I tried your function and wrote this to call it:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if (!@VALIDATE_USPHONE($_POST['phoneno'])){<br />Â Â Â Â $msg=$msg."Please enter a valid phone number.<br>";<br />Â Â Â Â $flag = "NOTOK";<br />}<!--c2--></div><!--ec2--><br /><br />When I entered in my form 123-456-7890 as the phone number, it should have let me submit the form since that is a valid format. <br /><br />However, it said "Please enter a valid phone number." Everything I was putting in was giving me the error.<br /><br />What did I do wrong? Is it something with the return value?<!--content-->
I'm a VERY NEW php person, but I would probably try putting in a javascript popup or something for testing purposes that will display the value of the $phonenumber variable. <br /><br />I'd want to know how the information was being entered into the variable to see if that helped to illustrate where the problem was.<br /><br />I dunno. Bruce (or somebody else) will probably be able to help you more than that. Just a small suggestion for a PHP beginner. (Who knows. Maybe you already thought of that and this is way over my head...)<!--content-->
<!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec-->Bruce-<br />First off, thanks for the code.<!--QuoteEnd--></div><!--QuoteEEnd--><br />Sorry, I found the code snippit in another forum. I am a PHP noob as well.<br /><br />Maybe one of our more well versed PHP members can point you in the right direction.<!--content-->
I believe it has something to do with the returning value. But I'm not quite sure how to handle it...<!--content-->
Well, once again I've figured out my problem. I think it was a combination of the returning values and how the code snippet was originally written. I revised the function to this:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function VALIDATE_USPHONE($phonenumber){<br />Â Â Â Â if ( (preg_match("/^[0-9]{3,3}[-]{1,1}[0-9]{3,3}[-]{1,1}[0-9]{4,4}$/", $phonenumber) ) == TRUE ){<br />Â Â Â Â Â Â Â Â Â Â return TRUE;<br />Â Â Â Â } else{<br />Â Â Â Â Â Â Â Â return FALSE;<br />Â Â Â Â }<br />}<!--c2--></div><!--ec2--><br /><br />and my function call to this:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if (!@VALIDATE_USPHONE($_POST['phoneno'])){<br />$msg=$msg."Please enter a valid phone number.<br>";<br />$flag = "NOTOK";<br />}<!--c2--></div><!--ec2--><br /><br />And it worked fine. Thanks for everyone's help.<!--content-->
Glad you got it working <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/thumbup1.gif" style="vertical-align:middle" emoid=":thumbup1:" border="0" alt="thumbup1.gif" /><br /><br />And if you are only interested in US phone numbers you can tweak it to <br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function VALIDATE_USPHONE($phonenumber){<br />Â Â Â Â if ( (preg_match("/^[3-9]{3,3}[-]{1,1}[0-9]{3,3}[-]{1,1}[0-9]{4,4}$/", $phonenumber) ) == TRUE ){<br />Â Â Â Â Â Â Â Â Â Â return TRUE;<br />Â Â Â Â } else{<br />Â Â Â Â Â Â Â Â return FALSE;<br />Â Â Â Â }<br />}<!--c2--></div><!--ec2--><br /><br />No US phone number begin with 0 or 1 in the area code.<!--content-->
Don't know about your code but here is an example I found that does what you want.<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->A very simple Phone number validation function.<br />Returns the Phone number if the number is in the xxx-xxx-xxxx format. x being 0-9.<br />Returns false if missing digits or improper characters are included.<br /><br /><?<br />function VALIDATE_USPHONE($phonenumber)<br />{<br />if ( (preg_match("/^[0-9]{3,3}[-]{1,1}[0-9]{3,3}[-]{1,1}<br />Â Â Â Â [0-9]{4,4}$/", $phonenumber) ) == TRUE ) {<br />Â Â return $phonenumber;<br /> } else {<br />Â Â return false;<br />Â Â }<br />}<br /><br />?><!--c2--></div><!--ec2--><!--content-->
Bruce-<br />First off, thanks for the code.<br /><br />I tried your function and wrote this to call it:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if (!@VALIDATE_USPHONE($_POST['phoneno'])){<br />Â Â Â Â $msg=$msg."Please enter a valid phone number.<br>";<br />Â Â Â Â $flag = "NOTOK";<br />}<!--c2--></div><!--ec2--><br /><br />When I entered in my form 123-456-7890 as the phone number, it should have let me submit the form since that is a valid format. <br /><br />However, it said "Please enter a valid phone number." Everything I was putting in was giving me the error.<br /><br />What did I do wrong? Is it something with the return value?<!--content-->
I'm a VERY NEW php person, but I would probably try putting in a javascript popup or something for testing purposes that will display the value of the $phonenumber variable. <br /><br />I'd want to know how the information was being entered into the variable to see if that helped to illustrate where the problem was.<br /><br />I dunno. Bruce (or somebody else) will probably be able to help you more than that. Just a small suggestion for a PHP beginner. (Who knows. Maybe you already thought of that and this is way over my head...)<!--content-->
<!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec-->Bruce-<br />First off, thanks for the code.<!--QuoteEnd--></div><!--QuoteEEnd--><br />Sorry, I found the code snippit in another forum. I am a PHP noob as well.<br /><br />Maybe one of our more well versed PHP members can point you in the right direction.<!--content-->
I believe it has something to do with the returning value. But I'm not quite sure how to handle it...<!--content-->
Well, once again I've figured out my problem. I think it was a combination of the returning values and how the code snippet was originally written. I revised the function to this:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function VALIDATE_USPHONE($phonenumber){<br />Â Â Â Â if ( (preg_match("/^[0-9]{3,3}[-]{1,1}[0-9]{3,3}[-]{1,1}[0-9]{4,4}$/", $phonenumber) ) == TRUE ){<br />Â Â Â Â Â Â Â Â Â Â return TRUE;<br />Â Â Â Â } else{<br />Â Â Â Â Â Â Â Â return FALSE;<br />Â Â Â Â }<br />}<!--c2--></div><!--ec2--><br /><br />and my function call to this:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if (!@VALIDATE_USPHONE($_POST['phoneno'])){<br />$msg=$msg."Please enter a valid phone number.<br>";<br />$flag = "NOTOK";<br />}<!--c2--></div><!--ec2--><br /><br />And it worked fine. Thanks for everyone's help.<!--content-->
Glad you got it working <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/thumbup1.gif" style="vertical-align:middle" emoid=":thumbup1:" border="0" alt="thumbup1.gif" /><br /><br />And if you are only interested in US phone numbers you can tweak it to <br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function VALIDATE_USPHONE($phonenumber){<br />Â Â Â Â if ( (preg_match("/^[3-9]{3,3}[-]{1,1}[0-9]{3,3}[-]{1,1}[0-9]{4,4}$/", $phonenumber) ) == TRUE ){<br />Â Â Â Â Â Â Â Â Â Â return TRUE;<br />Â Â Â Â } else{<br />Â Â Â Â Â Â Â Â return FALSE;<br />Â Â Â Â }<br />}<!--c2--></div><!--ec2--><br /><br />No US phone number begin with 0 or 1 in the area code.<!--content-->