I have a phone number field that is set up to validate using preg_match:
function ValidPhone($phone) {
return preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', trim($phone));
}
This ensures that phone numbers are entered in the xxx-xxx-xxxx format and that it’s numeric.
I’m not very familiar with regexp; can anyone tell me how I’d expand this to allow more than one format? i.e. xxx xxx xxxx or xxx.xxx.xxxx or (xxx) xxx-xxxx
Thanks.
I think the easiest solution would be to just strip out everything that isn’t numeric first:
Since you are validating the format, and not the content, this should do it: