27

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

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

+或 00 然后 6-14 个数字

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

这是我当前的代码:

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

BOOL phoneValidates = [phoneTest evaluateWithObject:phoneNumber];

我哪里错了?

谢谢!

4

9 回答 9

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

这种方式好一点。如果您转义“\”,您的代码也可以正常工作。

于 2012-11-01T12:11:17.000 回答
16

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

- (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

于 2015-05-20T19:41:28.453 回答
2

好吧,这取决于您想要的严格程度,这个正则表达式似乎并不是特别严格。这个正则表达式说:

从行首开始匹配一个 +(或者可能是 1 或 0),这似乎模棱两可(但可能不取决于实现),因为捕获括号:() 打破了 + 和 ? 可能放错了位置:匹配任何数字 0-9 1 或 0 次 6-14 次,然后是一个数字 0-9,然后是行尾。您还注意到,任何反斜杠都必须加倍...... @"\b" 用于单词边界。您可能想尝试类似...

@"\\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
于 2012-11-01T11:36:49.860 回答
2

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

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

于 2018-07-27T11:26:37.777 回答
1

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

 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));

用于格式化数字

-(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;
}

为了获得长度

-(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;
}
于 2014-02-10T10:39:57.283 回答
1

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

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

于 2016-09-24T13:27:10.623 回答
0

验证电话号码和密码限制的简单方法,只需按照以下过程。

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

之后,您可以实现文本文件委托方法“shouldChangeCharactersInRange”,只需编写此代码

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 是

#define kMobileNumberLimit 10
#define ALLOWED_NUMBERS [[NSCharacterSet decimalDigitCharacterSet] invertedSet]
#define minLimit 6
#define maxLimit 17
于 2017-03-01T09:28:26.207 回答
0

在迅速 4 -

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)
}
于 2018-07-26T09:17:44.527 回答
-4
-(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;

}

于 2016-09-24T11:13:02.233 回答