Reqular expression for 11 digit phone number with 09 prefix

后端 未结 3 595
独厮守ぢ
独厮守ぢ 2021-01-26 13:49

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)         


        
相关标签:
3条回答
  • 2021-01-26 14:32

    Using a NSString method:

    - (BOOL)validateAlphabets: (NSString *)text {
        NSString *regEx = @"09\\d{9}";
        NSRange range = [text rangeOfString:regEx options:NSRegularExpressionSearch];
        return range.location != NSNotFound;
    }
    
    0 讨论(0)
  • 2021-01-26 14:34

    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

    0 讨论(0)
  • 2021-01-26 14:42

    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;
    }
    
    0 讨论(0)
提交回复
热议问题