Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the regular expression for International phone number validation in php or zend?

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

like image 634
ehp Avatar asked Sep 23 '12 10:09

ehp


2 Answers

/^((\+|00)\d{1,3})?\d+$/

Try the above expression. hope this helps.

like image 157
user1559230 Avatar answered Sep 25 '22 16:09

user1559230


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 
like image 35
Tomáš Fejfar Avatar answered Sep 25 '22 16:09

Tomáš Fejfar