Sarah Posted December 14, 2005 Posted December 14, 2005 Good afternoon! 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. >function checkPhone($element){ $phoneno = trim($element); return !preg_match("(^(.*)[0-9]{3})(.*)([0-9]{3})(.*)([0-9]{4}$)", $phoneno); } I call this function in my code like this: >if (!@checkPhone($_POST['phoneno'])){ $msg=$msg."Please enter a valid phone number.<br>"; $flag = "NOTOK"; }else{ ....blah blah blah this code is if the phone number checks out ok } But when I run the function in my debugger, it shows me this warning: Debug Warning: validation.php line 29 - preg_match(): Unknown modifier '(' Any ideas? Thanks in advance. Quote
TCH-Bruce Posted December 14, 2005 Posted December 14, 2005 Don't know about your code but here is an example I found that does what you want. >A very simple Phone number validation function. Returns the Phone number if the number is in the xxx-xxx-xxxx format. x being 0-9. Returns false if missing digits or improper characters are included. <? function VALIDATE_USPHONE($phonenumber) { if ( (preg_match("/^[0-9]{3,3}[-]{1,1}[0-9]{3,3}[-]{1,1} [0-9]{4,4}$/", $phonenumber) ) == TRUE ) { return $phonenumber; } else { return false; } } ?> Quote
Sarah Posted December 14, 2005 Author Posted December 14, 2005 Bruce- First off, thanks for the code. I tried your function and wrote this to call it: >if (!@VALIDATE_USPHONE($_POST['phoneno'])){ $msg=$msg."Please enter a valid phone number.<br>"; $flag = "NOTOK"; } 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. However, it said "Please enter a valid phone number." Everything I was putting in was giving me the error. What did I do wrong? Is it something with the return value? Quote
abinidi Posted December 14, 2005 Posted December 14, 2005 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. 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. 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...) Quote
TCH-Bruce Posted December 14, 2005 Posted December 14, 2005 Bruce-First off, thanks for the code. Sorry, I found the code snippit in another forum. I am a PHP noob as well. Maybe one of our more well versed PHP members can point you in the right direction. Quote
Sarah Posted December 15, 2005 Author Posted December 15, 2005 I believe it has something to do with the returning value. But I'm not quite sure how to handle it... Quote
Sarah Posted December 16, 2005 Author Posted December 16, 2005 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: >function VALIDATE_USPHONE($phonenumber){ if ( (preg_match("/^[0-9]{3,3}[-]{1,1}[0-9]{3,3}[-]{1,1}[0-9]{4,4}$/", $phonenumber) ) == TRUE ){ return TRUE; } else{ return FALSE; } } and my function call to this: >if (!@VALIDATE_USPHONE($_POST['phoneno'])){ $msg=$msg."Please enter a valid phone number.<br>"; $flag = "NOTOK"; } And it worked fine. Thanks for everyone's help. Quote
TCH-Bruce Posted December 16, 2005 Posted December 16, 2005 Glad you got it working And if you are only interested in US phone numbers you can tweak it to >function VALIDATE_USPHONE($phonenumber){ if ( (preg_match("/^[3-9]{3,3}[-]{1,1}[0-9]{3,3}[-]{1,1}[0-9]{4,4}$/", $phonenumber) ) == TRUE ){ return TRUE; } else{ return FALSE; } } No US phone number begin with 0 or 1 in the area code. Quote
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.