regular expression for phone number in php

Chirayu picture Chirayu · Jun 11, 2011 · Viewed 16k times · Source

I am using zend form validation for a phone number. and I want, user should enter phone number in these formats:

"+91-151-1234567", "01234567891", "+912345678901"

so i am using this regular expression:

"^(?:\+?([0-9]{3})\)?[-. ]?\(?([0-9]{3})\)?[-. ]?([0-9]{10})[-. ]$"

but it is not working. it is generating error :

Fatal error: Uncaught exception 'Zend_Validate_Exception' with message 'Internal error matching pattern '^(?:+?([0-9]{3}))?[-. ]?(?([0-9]{3}))?[-. ]?([0-9]{10})[-. ]$' against value '+91-151-3297154'' in /var/www/html/allindiazend/library/Zend/Validate/Regex.php:117 Stack trace: #0 /var/www/html/allindiazend/library/Zend/Validate.php(98): Zend_Validate_Regex->isValid('+91-151-3297154')

1 /var/www/html/allindiazend/library/Zend/Validate.php(98):

Zend_Validate->isValid('+91-151-3297154')

2 /var/www/html/allindiazend/library/Zend/Filter/Input.php(932):

Zend_Validate->isValid('+91-151-3297154')

3 /var/www/html/allindiazend/library/Zend/Filter/Input.php(800):

Zend_Filter_Input->_validateRule(Array)

4 /var/www/html/allindiazend/library/Zend/Filter/Input.php(688):

Zend_Filter_Input->_validate() #5 /var/www/html/allindiazend/library/Zend/Filter/Input.php(430): Zend_Filter_Input->_process() #6 /var/www/html/allindiazend/application/controllers/StaticController.php(148): Zend_Filter_Input->isValid() #7 /var/www/html/allindi in /var/www/html/allindiazend/library/Zend/Validate/Regex.php on line 117

Can any one help me for making this.

thank you in advance.

Answer

Alex Aza picture Alex Aza · Jun 11, 2011

This expression will match all your 3 examples:

\+?([0-9]{2})-?([0-9]{3})-?([0-9]{6,7})