This forum is no longer open and is for reading/searching only.
Please use our new MachForm Community Forum instead.
MachForm Community Forums » MachForm 2
Changing phone number validation
Started 16 years ago by philwareham | 4 posts |
-
Hi there,
I wish to use the 'phone' field in a form on a UK-centric website, where there are numerous ways people can enter their phone number (for instance some numbers are ##### ###### whilst others are #### ### ##### etc). Having set the field option to 'international' I'm still getting an error message on submit to enter a valid phone number.
Is there any way of manually adjusting the code to accommodate this? I would use the simple 'numbers' field but that does not seem to like the use of spaces.
Thanks,
PhilPosted 16 years ago # -
The 'international' phone format accept +XXXXXXX or XXXXXX numbers.
The minimum length is 3 numbers and spaces are not allowed.If you would like to allow spaces, edit your includes/common-validator.php file.
Search around line 145 for this code:function validate_simple_phone($value){ $error_message = VAL_PHONE; if($value[0]{0} == '+'){ $test_value = substr($value[0],1); }else{ $test_value = $value[0]; } if(is_numeric($test_value) && (strlen($test_value) > 3)){ return true; }else{ return $error_message; } }
replace it with this one:function validate_simple_phone($value){ $error_message = VAL_PHONE; if($value[0]{0} == '+'){ $test_value = substr($value[0],1); }else{ $test_value = $value[0]; } $test_value = str_replace(" ","",$test_value); if(is_numeric($test_value) && (strlen($test_value) > 3)){ return true; }else{ return $error_message; } }
that should allow spaces.MachForm Founder
Posted 16 years ago # -
That's great, thanks yuniar.
Can I scrounge one more thing... is there any chance you could supply the above example but also including support for "-" character in the phone number?
Thanks a lot,
PhilPosted 16 years ago # -
The easiest way to allow "-" character would be this:
function validate_simple_phone($value){ $error_message = VAL_PHONE; if($value[0]{0} == '+'){ $test_value = substr($value[0],1); }else{ $test_value = $value[0]; } $test_value = str_replace(array(" ","-"),"",$test_value); if(is_numeric($test_value) && (strlen($test_value) > 3)){ return true; }else{ return $error_message; } }
MachForm Founder
Posted 16 years ago #
Reply
You must log in to post.