Validate phone number ios

前端 未结 9 1744
温柔的废话
温柔的废话 2020-12-10 00:42

I need to validate an international phone number.

I know its difficult to validate an international phone number, so I\'m going to keep it simple:

+

相关标签:
9条回答
  • 2020-12-10 01:20
    NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}$";
    

    This way is bit better. Your code will work as well if you escape the "\".

    0 讨论(0)
  • 2020-12-10 01:22

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

    start at beginning of line 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 ? possibly misplaced : match any digit 0-9 1 or 0 times 6-14 times then one digit 0-9 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)
  • 2020-12-10 01:22

    Easy way to validate phone number and password limit , just follow the below process.

    if ((self.mobileTxtFld.text.length < 10 )) {
        [_mobileTxtFld becomeFirstResponder];
    }
    else if ((self.passwordTxtFld.text.length < kPasswordCharacterMinLimit) || (self.passwordTxtFld.text.length > kPasswordCharacterMaxLimit)) {
        // show alert
    }
    

    after that you can implement textfiled delegate method "shouldChangeCharactersInRange" in that just write this code

    if (textField == _mobileTxtFld) {
        if([string rangeOfCharacterFromSet:ALLOWED_NUMBERS].location == NSNotFound){
            NSUInteger newLength = [textField.text length] + [string length] - range.length;
            if(newLength > kMobileNumberLimit - 1){
                if(textField.text.length != kMobileNumberLimit){
                    textField.text  = [NSString stringWithFormat:@"%@%@",textField.text,string];
                }
                [textField resignFirstResponder];
                return NO;
            }
            else{
                return YES;
            }
        }
        else{
            return NO;
        }
    }
    
    return YES;
    

    here ALLOWED_NUMBERS and kMobileNumberLimit are

    #define kMobileNumberLimit 10
    #define ALLOWED_NUMBERS [[NSCharacterSet decimalDigitCharacterSet] invertedSet]
    #define minLimit 6
    #define maxLimit 17
    
    0 讨论(0)
  • 2020-12-10 01:30

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

    This is tested RegularExpression This will accept with country code OR without country code

    0 讨论(0)
  • 2020-12-10 01:39

    The only good way to validate phone numbers is to use Google's amazing LibPhoneNumber. There's an iOS port, or you can run the JavaScript implementation in a hidden UIWebView.

    (I've done the latter years ago when their was no iOS port yet. Works like a charm and is very fast even on old iPhones.)

    0 讨论(0)
  • 2020-12-10 01:41
    -(int)findLength:(NSString*)phoneNumber
    {
        phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
        phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
        phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
        phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
        phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
    
    int length = [phoneNumber length];
    
    return length;
    

    }

    0 讨论(0)
提交回复
热议问题