1

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.

Community
  • 1
  • 1
Chirayu
  • 4,693
  • 7
  • 28
  • 46
  • 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'' – Chirayu Jun 11 '11 at 06:27
  • Do you want to catch other regional formats? Such as "+44 (12345) 12345678", "+353 12 1234567", "+61 1 12345678", "+33.1.23.45.67.89", ...? – johnsyweb Jun 11 '11 at 06:47
  • This is such a difficult thing to achieve. Not only do you have to handle spaces etc, but some people add "ext 343" at the end etc. If you have a defined format that is fine, but if you are asking people to put in a phone number you are in for a world of pain if you don't just leave it as text. – Cameron Mar 22 '18 at 23:26

4 Answers4

5

This expression will match all your 3 examples:

\+?([0-9]{2})-?([0-9]{3})-?([0-9]{6,7})
Alex Aza
  • 76,499
  • 26
  • 155
  • 134
4

Not sure what part of yours isn't matching, but I just tested an alternative version I wrote and it seems to work for all your examples:

^(\+\d{12}|\d{11}|\+\d{2}-\d{3}-\d{7})$
Alexander Tsepkov
  • 3,946
  • 3
  • 35
  • 59
2

Instead of doing extensive checking of all allowed formats, I would allow any format. You can convert it to a single format (without dashes and parentheses) do all the checking you want, and store each phone number in the same way, so that you can easily retrieve it as well.

If you solve your problem this way, your interface is more user friendly, additional checks are more easily added and your stored phone numbers are better usable for other applications.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

Should I create a php script that extracts all the phone numbers listed on a page ...

The numbers may be written in different formats such as: 0039024343333 +39024343333 (0041) 91 999 11 11 +41 (0) 91 999 11 11 0919991111 091 99 911 11 +1 123 344 2244 5 123-344-2244-5 etc..

I have tried this script but it works only in part:

GetPhoneNumber function ($ txt) {
$ regexp = '/ ([+ \ \ s]) {1,3} ([0-9 \ \ s] {2,5}) -? ([0-9 \ \ s] {2,5}) -? ([0-9 \ \ s] {2,20}) / ';

preg_match_all ($ regexp, $ txt, $ m);


return isset ($ m [0])? $ m [0]: array ();
}

$ fulltxt = file_get_contents ('http://wiki.wikimedia.it/wiki/Contatti');
$ phonenumber = GetPhoneNumber ($ fulltxt);
print_r ($ phonenumber);