konfused Posted March 23, 2013 Share Posted March 23, 2013 In my registration form I filter the UK phone number as follows:// Has a phone number been entered? if (!empty($_POST['phone'])) { //Remove spaces, hyphens, and brackets.$phone=str_replace(array(' ', '-', '(', ')', ')'), '',$_POST['phone']);}//line 212//Use regex to check that the remaining characters are digitsIf (preg_match('/^[0-9]{11}$/', $phone)){$ph=$phone;}else{$errors[]= 'Phone numbers may only contain spaces, brackets, hyphens and the digits 0 to 9, please correct it';}if (empty($_POST['phone'])){$ph=NULL;}If the number is entered by the user it registers without a problem.If a letter is inserted among the numbers it displays the error message correctly .If a space is inserted it displays the error message, but it shouldn'tThe phone field is set at NULL in the table because some users are ex-directory and prefer not to declare their phone numbers.However, if the field is empty it displays the error message, but it shouldn't, and it also puts up a notice: "Undefined variable in line 212".1. Please would tell me how to correct the code.2. Some UK numbers have 10 digits, how do I allow that? Link to comment Share on other sites More sharing options...
margaux Posted March 24, 2013 Share Posted March 24, 2013 Try declaring $phone = ''; before the first if statement. You may want to double check your regex because I did not get the results you described. Also check that you are using null and empty correctly as they do not always equate. Finally - please use code tags for your code. Link to comment Share on other sites More sharing options...
HartleySan Posted March 24, 2013 Share Posted March 24, 2013 konfused, if you want to filter all non-digits out of a phone number, use the following regex replace: $new_str = preg_replace('/\D+/', '', $old_str); This will remove ALL non-digit characters from the string. From there, you should be able to do any testing that is necessary. 1 Link to comment Share on other sites More sharing options...
konfused Posted March 24, 2013 Author Share Posted March 24, 2013 Thank you Margaux and HartleySan Adding $phone = ''; did not improve things. I used HarleySans suggestion and that resolved the issue. I don't understand what you mean by "use code tags", I did use tags, do you mean that I should change all tags to entities before submitting a query? regards Konfused Link to comment Share on other sites More sharing options...
Antonio Conte Posted March 24, 2013 Share Posted March 24, 2013 Jon: Did not know that. Thanks for the snippet. konfused: As in the forum's code tags. They'll format code for you. Look for the "<>" symbol. Look at this: // Has a phone number been entered? if (!empty($_POST['phone'])) { //Remove spaces, hyphens, and brackets. $phone=str_replace(array(' ', '-', '(', ')', ')'), '',$_POST['phone']);} //line 212 //Use regex to check that the remaining characters are digits If (preg_match('/^[0-9]{11}$/', $phone)){ $ph=$phone; } } Versus this: // Has a phone number been entered? if (!empty($_POST['phone'])) { //Remove spaces, hyphens, and brackets.$phone=str_replace(array(' ', '-', '(', ')', ')'), '',$_POST['phone']);}//line 212//Use regex to check that the remaining characters are digitsIf (preg_match('/^[0-9]{11}$/', $phone)){$ph=$phone;} Link to comment Share on other sites More sharing options...
konfused Posted March 31, 2013 Author Share Posted March 31, 2013 Thanks to everyone. Using Hartley San's digits-only solution solved the problem for USA and UK phone numbers. I can now see the "<>" symbol and will use it in future. Link to comment Share on other sites More sharing options...
Recommended Posts