0

Telephone number should be international and user has to enter the complete phone number with country code.

For that I need a regex for formatting the phone number.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Madan Mohan
  • 8,764
  • 17
  • 62
  • 96

1 Answers1

4

For real regex testing use RegexKitLite.

As for the regular expression itself, something like this (from Validate Phone Numbers: A Detailed Guide) should work:

^\+(?:[0-9] ?){6,14}[0-9]$

Note that when you specify it in your code you need to escape the backslash character (\), so it would look like this:

NSString *regexString = @"^\\+(?:[0-9] ?){6,14}[0-9]$";
Matthew Frederick
  • 22,245
  • 10
  • 71
  • 97
  • Thank you its simple and nice – Madan Mohan Nov 29 '10 at 08:23
  • Glad you like it. If this answer helped you, please check the checkmark next to it. – Matthew Frederick Nov 29 '10 at 08:29
  • If user want to add this symbol "-" and alphabets for example 1-800-MY-APPLE then how to validate for given format – Madan Mohan Nov 29 '10 at 08:59
  • @ Matthew Frederick:If user want to add this symbol "-" and alphabets for example 1-800-MY-APPLE then how to validate for given format. And how many character do we need to include that is greater than 12 characters – Madan Mohan Nov 29 '10 at 09:08
  • I believe you can replace the space (right after the first [0-9]) with [ \-] which would mean a space or a -. In the NSString you would need to escape the backslash: [ \\-] . – Matthew Frederick Nov 29 '10 at 09:10
  • As noted in the link to the Validate Phone Numbers blog entry, this will validate one or more numbers followed by a space (now a space or a dash), and require that these groups be there 6 to 14 times, ending with a number. – Matthew Frederick Nov 29 '10 at 09:13