How to Validation phone number and -

前端 未结 5 2027
星月不相逢
星月不相逢 2021-01-14 23:26

For the validation for the phone number and included -

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

I have to validate phone number with -

example 333-333-3333 (

相关标签:
5条回答
  • 2021-01-15 00:02

    Both of the other answers will work, but they fail in significant ways:

    1. They are not internationalized. They don't account for parentheses around the area code, or the country code, or spaces instead of hyphens. All of these are valid permutations of phone numbers.
    2. They're fragile. What if someone doesn't want to enter an area code? What if something changes and all phone numbers in the US suddenly gain an extra digit? Will your code break?

    The answer to this is to let the frameworks handle this for you. There's a class called NSDataDetector on iOS 4+ and OS X 10.7+ that you can use for detecting physical addresses, email addresses, links, transit information, phone numbers, etc.

    The documentation gives an example of how to use it. This is a much better solution because it's code that you don't have to maintain. It will work anywhere in the world.

    0 讨论(0)
  • 2021-01-15 00:13

    //phone no format:(000)000-0000

    -(BOOL)numberValidation:(NSString*)phoneString
    {
    
        NSString *phoneRegEx = @"\\([0-9]{3}\\)[0-9]{3}\\-[0-9]{4}";
    
        NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegEx];
    
        BOOL result=[phoneTest evaluateWithObject:phoneString];
       return result;
    
    }
    
    0 讨论(0)
  • 2021-01-15 00:14

    Just suggesting another way, as mentioned by @Dave DeLong as well NSDataDetector. One of the best and elegant way provided by Apple. Who might be looking for the implementation, here is the code snippet.

    - (BOOL)applyPhoneValidation:(NSString*)theNumber
    {
        NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:NULL];
        NSArray *matches = [detector matchesInString:theNumber options:0 range:NSMakeRange(0, theNumber.length)];
        BOOL isValidNumber = (NSInteger)matches.count;
        return isValidNumber;
    }
    

    It will return 1 for the valid phone number.

    Cheers Sanjay

    0 讨论(0)
  • 2021-01-15 00:15

    Obj-C has NSPredicate for this. As I remember, you can use code like this for your task:

    NSPredicate * Pred = [NSPredicate predicateWithFormat:@"SELF MATCHES '^\+(?:[0-9] ?){6,14}[0-9]$'"];    
    bool ifYes = [Pred evaluateWithObject:_yourNSString]];
    

    for more detail look in Apple docs.

    If you want to found proper reg.exp., please use some libs like: http://regexlib.com/DisplayPatterns.aspx?cattabindex=6&categoryId=7

    0 讨论(0)
  • 2021-01-15 00:24

    well it depends on how strict you want to be it doesn't seem like this regex is especially strict. this regex says:

    1. start at beginning of line
    2. match one + (or maybe 1 or 0) which seems ambiguous (but may not be depending on implementation) because the capture parentheses:() breaks up the relationship of the \+ and the ?
    3. possibly misplaced :
    4. match any digit 0-9 1 or 0 times 6-14 times
    5. then one digit 0-9
    6. then end of line.

    also you note that any backslash will have to be doubled... @"\\b" for a word boundary. you may want to try something like...

    @"\\b[\\d]{3}\\-[\\d]{3}\\-[\\d]{4}\\b"
    would I think match your example, but it wouldn't match
    (555) 555 - 5555 or
    555.555.5555 or
    +44 1865  55555
    
    0 讨论(0)
提交回复
热议问题