Regular expression -- mobile phone number, letters, numbers, password mixed judgment

Mobile phone number, letter, number, password mixed judgment, regular expression, do you need it?

Verify letters ^[a-zA-Z]*$

// 正则
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[a-zA-Z]*$" options:NSRegularExpressionAnchorsMatchLines error:nil];

    NSUInteger numberOfMatches = [regex numberOfMatchesInString:[textField text] options:NSMatchingAnchored range:NSMakeRange(0, [[textField text] length])];

Validation numbers^[0-9]*$

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]*$" options:NSRegularExpressionAnchorsMatchLines error:nil];

    NSUInteger numberOfMatches = [regex numberOfMatchesInString:[textField text] options:NSMatchingAnchored range:NSMakeRange(0, [[textField text] length])];

Verify phone number^((13)|(14)|(15)|(17)|(18))\d{9}$

// 验证手机号 - 正则表达式
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^((13)|(14)|(15)|(17)|(18))\\d{9}$" options:NSRegularExpressionAnchorsMatchLines error:nil];

    NSUInteger numberOfMatches = [regex numberOfMatchesInString:[textField text] options:NSMatchingAnchored range:NSMakeRange(0, [[textField text] length])];

    if (numberOfMatches == 0) {

        // 弹出提示框 - 显示提示信息
        [self alertViewController:@"您的手机号输入有误"];
    }else{
        NSLog(@"正确");
    }
    return numberOfMatches;

Verify the mixture of letters and numbers – It can be used when it is judged that the password contains a mixture of letters and numbers. It is very convenient and can be used directly.

// 验证字母和数字混合 -- 密码
-(void)checkIsHaveNumAndLetter:(NSString*)password{
    //数字条件
    NSRegularExpression *tNumRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];

    //符合数字条件的有几个字节
    NSUInteger tNumMatchCount = [tNumRegularExpression numberOfMatchesInString:password
                                                                       options:NSMatchingReportProgress
                                                                         range:NSMakeRange(0, password.length)];

    //英文字条件
    NSRegularExpression *tLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]" options:NSRegularExpressionCaseInsensitive error:nil];

    //符合英文字条件的有几个字节
    NSUInteger tLetterMatchCount = [tLetterRegularExpression numberOfMatchesInString:password options:NSMatchingReportProgress range:NSMakeRange(0, password.length)];

    if (tNumMatchCount == password.length) {
        //全部符合数字,表示沒有英文
        // 调用提示框的方法 - 将要显示的内容传到提示框上展示出来
        [self alertViewController:@"您输入的密码中不包含英文"];
        return;
    } else if (tLetterMatchCount == password.length) {
        //全部符合英文,表示沒有数字
        [self alertViewController:@"您输入的密码中不包含数字"];
        return;
    } else if (tNumMatchCount + tLetterMatchCount == password.length) {
        //符合英文和符合数字条件的相加等于密码长度
//        return;
    } else {
        [self alertViewController:@"您输入的密码中含有特殊符号"];
        return;
        //可能包含标点符号的情況,或是包含非英文的文字,这里再依照需求详细判断想呈现的错误
    }

}

A method of writing a prompt box - easy to call repeatedly

// 定义提示框方法
-(void)alertViewController:(NSString *)text
{
    // 创建提示框
    UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:text message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alertCon addAction:action];
    [self presentViewController:alertCon animated:YES completion:nil];
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324671713&siteId=291194637