3

Hi I need phone number validation for France.

Valid format : +262#########

Validation done in textDidChang: so every I need to check every number belong to above number.

My Regex below

[[NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"+2?6?2?\d?\d?\d?\d?\d?\d?\d?\d?\d?"] evaluateWithObject:@"+262989878989"]

While execute this line app crash

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do regex matching, reason: (Can't open pattern U_REGEX_RULE_SYNTAX (string 2, pattern +2?6?2?\d?\d?\d?\d?\d?\d?\d?\d?\d?, case 0, canon 0))'

Help me to fix this.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
TamilKing
  • 1,643
  • 1
  • 12
  • 23
  • What are the rules for French phone numbers? They start with 262 and have 9 digits following it? A total of 12? – NSNoob Oct 05 '15 at 09:36
  • @NSNoob: yes, https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSRegularExpression_Class/ here I read "\d" for get number why it not working.. – TamilKing Oct 05 '15 at 09:46
  • See Abhinav's answer. it should work. Just add the validation rule for 00 numbers as well as + numbers – NSNoob Oct 05 '15 at 09:47
  • @TamilKing I believe your question was something else. What you want is to validate user phone number input at each digit typed in. Please take a look at my updated post - though a non-regex way! – Abhinav Oct 05 '15 at 10:18

3 Answers3

3

The \d is not working because it's in a string literal, and you have to escape the \ (e.g. @"\\d"). You should also escape the + because that has a special meaning in regex, too (e.g. @"^\\+262\\d{9}$").

Note the ^ and $ which match the start and end of the string, if that's what you meant. Or if you're looking for this anywhere in the larger string, you should check for word boundaries. Without one of these two approaches, you'll get false positive with +262 followed more than nine characters.

--

If you're trying to make sure someone enters a number that matches this format, you can do something like:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];

    return [[NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^(\\+(2(6(2\\d{0,9})?)?)?)?$"] evaluateWithObject:result];
}

That will match any string that is the beginning of a valid phone number.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanks.. While type I need to check the string start with correct number can you pls help me to achieve this? – TamilKing Oct 05 '15 at 09:56
  • I need to check while type that is in textDidChange: if user type "+263" I need to restrict the last number "3".. – TamilKing Oct 05 '15 at 09:59
  • @TamilKing - OK, I get you now. You could do something like `@"^(\\+(2(6(2\\d{0,9})?)?)?)?$"`. I know that's a little hairy, but you can't just `?` the individual characters, but rather do it from the end with `(` `)`. See revised answer, assuming you're trying to do this in a `UITextFieldDelegate` method. You should be able to adapt it if you're using it in another context. – Rob Oct 05 '15 at 10:39
  • I posted my own answer.. Thanks for your response. – TamilKing Oct 06 '15 at 04:16
1
[[NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"\\+2?6?2?\\d?\\d?\\d?\\d?\\d?\\d?\\d?\\d?\\d?"] evaluateWithObject:phoneNumber];

phoneNumber will be +2, +26, +262, ....... +262#########..

Before + I need to put \\. That is my mistake.

TamilKing
  • 1,643
  • 1
  • 12
  • 23
  • This is not correct. This will also match `+6999999999` or `+2999999999` or even `+999999999`. Also, you want `\\d`, not `\d`. – Rob Oct 06 '15 at 04:27
0
NSString *phoneNumber = ...;

NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}$";

NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex]; 
BOOL matches = [test evaluateWithObject:phoneNumber];
user4261201
  • 2,324
  • 19
  • 26
Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
  • It looks like some copy paste issue here. I guess while editing my post, same code get pasted to your post. Could you please check and revert back to your original suggestion. I apologies for inconvenience. – Abhinav Oct 05 '15 at 10:22