关于iphone:验证电话号码ios

Validate phone number ios

我需要验证一个国际电话号码。

我知道很难验证国际电话号码,所以我会保持简单:

+或00然后是6-14个数字

我使用正则表达式的当前代码由于某些我无法解决的原因而无法正常工作。 它只是说它无法打开正则表达式和崩溃。

这是我目前的代码:

1
2
3
4
NSString *phoneRegex = @"^[\+(00)][0-9]{6,14}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];

BOOL phoneValidates = [phoneTest evaluateWithObject:phoneNumber];

我哪里错了?

谢谢!


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

这种方式有点好。如果你逃避"",你的代码也会正常工作。


复制&粘贴方法来验证电话号码:

1
2
3
4
5
6
7
- (BOOL)validatePhone:(NSString *)phoneNumber
{
    NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];

    return [phoneTest evaluateWithObject:phoneNumber];
}

或用于验证电话号码的开源库

https://github.com/iziz/libPhoneNumber-iOS


这取决于你想要的严格程度,看起来这个正则表达式并不是特别严格。这个正则表达式说:

从行首开始
匹配一个+(或者可能是1或0)似乎含糊不清(但可能不依赖于实现),因为捕获括号:()打破了+和?的关系?
可能放错地方:
匹配任何数字0-9 1或0次6-14次
然后一位数0-9
然后结束。
还要注意,任何反斜杠都必须加倍... @" b"表示单词边界。你可能想试试像...

1
2
3
4
5
@"\\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


txtlpmobile.text是字符串(手机没有输入)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 int length = [self getLength:txtLpMobile.text];
            if(length == 10) {
                if(range.length == 0)
                    return NO;
            }
            if(length == 3){
                NSString *num = [self formatNumber:txtLpMobile.text];
                txtLpMobile.text = [NSString stringWithFormat:@"(%@)",num];

                if(range.length > 0) {
                    txtLpMobile.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];

                }
            } else if(length == 6) {
                NSString *num = [self formatNumber:txtLpMobile.text];
                txtLpMobile.text = [NSString stringWithFormat:@"(%@) %@-",[num  substringToIndex:3],[num substringFromIndex:3]];
                if(range.length > 0) {
                    txtLpMobile.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
                }
            }

            NSUInteger newLength;
            newLength = [txtLpMobile.text length] + [string length] - range.length;
            NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS_ONLY] invertedSet];
            NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
            return (([string isEqualToString:filtered])&&(newLength <= CHARACTER_LIMIT));

用于格式化数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-(NSString*)formatNumber:(NSString*)mobileNumber
{
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

    int length = [mobileNumber length];
    if(length > 10)
    {
        mobileNumber = [mobileNumber substringFromIndex: length-10];
    }
    return mobileNumber;
}

获得长度

1
2
3
4
5
6
7
8
9
10
11
12
-(int)getLength:(NSString*)mobileNumber
{
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

    int length = [mobileNumber length];

    return length;
}


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

这是经过测试的RegularExpression
这将接受国家代码或没有国家代码


在快速4 -

1
2
3
4
5
6
7
8
9
10
func ValidateMobileNumber(txtFeid : UITextField, strChk : String, range: NSRange) -> Bool {

    if txtFeid.text!.count >= 10 {
        return false
    }

    let formatePre ="^((\\+)|(00))[0-9]{6,14}$"
    let resultPredicate : NSPredicate = NSPredicate(format:"SELF MATCHES %@",formatePre)
    return resultPredicate.evaluate(with: strChk)
}


验证电话号码和密码限制的简便方法,请按照以下流程进行操作。

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

之后你可以实现textfiled委托方法"shouldChangeCharactersInRange",只需编写这段代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;

这里有ALLOWED_NUMBERS和kMobileNumberLimit

1
2
3
4
#define kMobileNumberLimit 10
#define ALLOWED_NUMBERS [[NSCharacterSet decimalDigitCharacterSet] invertedSet]
#define minLimit 6
#define maxLimit 17

验证电话号码的唯一好方法是使用Google惊人的LibPhoneNumber。有一个iOS端口,或者您可以在隐藏的UIWebView中运行JavaScript实现。

(我已经完成了几年前,当时他们还没有iOS端口。就像魅力一样,即使在旧的iPhone上也很快。)


1
2
3
4
5
6
7
8
9
10
11
-(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;

}