I have a zend form where I have a phone number field and have to check for validator.
I have decided to use regular expression for that. I searched google but the results I have is not working.
Can anyone please provide me the regular expression. here is my code:
$phone = new Zend_Form_Element_Text('phone');
$phone->setRequired(true);
$phone->setLabel('Phone :')
->addFilter('StripTags')
->addValidator('NotEmpty', false, array('messages'=>'phone cannot be empty'))
->addFilter('StringTrim')
->addValidator('regex', false, array('/^[0-9 ]+$/','messages'=>'not a valid phone number'))
->addValidator('StringLength', false, array(5, 25, 'messages'=>'phone must be 5-25 character'))
Thanks in advance
/^((\+|00)\d{1,3})?\d+$/
Try the above expression. hope this helps.
Please note that phone number is one of the hardest everyday data validations that exist (along with email, that can contain "+" for example or port number in domain). You should understand the implications of using "some" regex. There may be some users (even whole countries) that won't match some regexes. Or users can input numbers, that are not phone numbers even though they match the regex.
http://en.wikipedia.org/wiki/E.164
There's been an issue about Zend_Phone to implement E.164. But I was not implemented in the end. The accepted solution (/^((\+|00)\d{1,3})?\d+$/
) would match even next string as valid phone:
+0000000000000000000000000000000000000000000000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With