iOS开发——手机号,密码,邮箱,身份证号,中文判断

   

     目前这些方面的判断主要是用了正则表达式

     

    手机号的判断,目前主要是长度、均是数字,支持的号段由于第三方通讯比如京东通讯,小米通讯等支持的号段挺多,

    有171,170,135,147等等,所以号段限制简单即可。

   @"^((13[0-9])|(147)|(17[0-9])|(15[^4,\D])|(18[0,0-9]))\d{8}$"

    其实手机号格式主要是为了短信验证,如果更简单点保证11位数字即可。

    密码验证的条件比较多,除了位数,是否包含空格(用string方法单独判断空格),有个可能是符号啊,正角半角啊。至少包含两种字符啊。

    NSString * regex = @"^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,18}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
       BOOL isMatch = [pred evaluateWithObject:self.passwordField.currentString];
        if (isMatch == NO) { // 不符合上面的正则,提示
          
          请输入6-18位字符。仅支持数字、大写字母、小写字母和特殊符号,且至少包含其中2种
           
            return;
      }

      但密码支持的较多字符时,有时可以单独判断密码符合  密码必须为unicode 0~127

          NSString * regex = @"^.*[\u0000-\u007f]{6,18}.*$";
            NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
            BOOL isMatch = [pred evaluateWithObject:self.passwordField.currentString];
            if (isMatch ==NO) {
                
                [BlackShowView alertViewShowInContentView:self.contentView message:@"请不要输入过于特殊的字符作为密码"];
                
                return;
            }

 

    邮箱的判断比较简单

    @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";

    身份证号

    @"^(\d{14}|\d{17})(\d|[xX])$";

   还有有个比较常用的是判断是否输入了中文(注意此时是YES的状态,及不能包含中文)

       // 是否包含中文^.*[u4e00-u9fa5].*$
        {
            NSString * regex = @"^.*[u4e00-u9fa5].*$";
            NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
            BOOL isMatch = [pred evaluateWithObject:self.accountField.currentString];
            if (isMatch ==YES) {
                
                [BlackShowView alertViewShowInContentView:self.contentView message:@"账号不能包含中文"];
                
                return;
            }
        
        
        }

原文地址:https://www.cnblogs.com/LiuChengLi/p/5179980.html