Reqular expression for 11 digit phone number with 09 prefix

拥有回忆 提交于 2020-01-22 02:53:04

问题


I am trying to write a regular expression to handle phone number which starts with "0" followed by "9" and 9 digits which can be anything within 0-9.

-(BOOL) validateAlphabets: (NSString *)text{      
     NSString *Regex = @"what should be here?!";
     NSPredicate *TestResult = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
     return [TestResult evaluateWithObject:text];
}

Then I check the input validation by:

if (![self validateAlphabets:self.phoneNumber.text]){
      NSLog(@"Invalid");
   }
else{
      NSLog(@"Valid!");
 }

回答1:


You can try to use this Regex:

09[0-9]{9}

REGEX DEMO

You can try to test it in your function itself like this:

-(BOOL) validateAlphabets: (NSString *)text{      
     NSString *Regex = @"what should be here?!";
     NSPredicate *TestResult = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
     if(TestResult evaluateWithObject: text)
     return true;
     else
     return false;
}



回答2:


I would go with

(0|۰)(9|۹)(\d|[dummy]){9} //<-- \d being any digit

Replace dummy with the numbers of the alphabet you want to support




回答3:


Using a NSString method:

- (BOOL)validateAlphabets: (NSString *)text {
    NSString *regEx = @"09\\d{9}";
    NSRange range = [text rangeOfString:regEx options:NSRegularExpressionSearch];
    return range.location != NSNotFound;
}


来源:https://stackoverflow.com/questions/32204210/reqular-expression-for-11-digit-phone-number-with-09-prefix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!