zoukankan      html  css  js  c++  java
  • iOS 身份证,邮箱,手机号验证

     1 //邮箱验证
     2 + (BOOL) validateEmail:(NSString *)email
     3 {
     4     NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
     5     NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
     6     return [emailTest evaluateWithObject:email];
     7 }
     8 
     9 //身份证号
    10 + (BOOL) validateIdentityCard: (NSString *)identityCard
    11 {
    12     BOOL flag;
    13     if (identityCard.length <= 0) {
    14         flag = NO;
    15         return flag;
    16     }
    17     NSString *regex2 = @"^(\d{14}|\d{17})(\d|[xX])$";
    18     NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    19     return [identityCardPredicate evaluateWithObject:identityCard];
    20 }
    21 
    22 //数字验证
    23 + (BOOL)validateNumber:(NSString*)number
    24 {
    25     BOOL res = YES;
    26     NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    27     int i = 0;
    28     while (i < number.length) {
    29         NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
    30         NSRange range = [string rangeOfCharacterFromSet:tmpSet];
    31         if (range.length == 0) {
    32             res = NO;
    33             break;
    34         }
    35         i++;
    36     }
    37     return res;
    38 }
    39 /**
    40  *  正则表达式验证手机号
    41  *
    42  *  @param mobile 传入手机号
    43  *
    44  *  @return
    45  */
    46 - (BOOL)validateMobile:(NSString *)mobile
    47 {
    48     // 130-139  150-153,155-159  180-189  145,147  170,171,173,176,177,178
    49     NSString *phoneRegex = @"^((13[0-9])|(15[^4,\D])|(18[0-9])|(14[57])|(17[013678]))\d{8}$";
    50     NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    51     return [phoneTest evaluateWithObject:mobile];
    52 }
  • 相关阅读:
    fetch的使用--当无法判断后台返回数据为什么类型时如何操作
    单页面与多页面间的区别及优缺点
    关于倒计时在关屏后不准确的问题
    前端分页仿百度分页效果
    pc端的弹性布局适配方案
    前端性能优化方向
    居民身份证号码组成规则
    axios简单介绍
    es6 promise 简单总结
    js原型链和原型链的继承
  • 原文地址:https://www.cnblogs.com/kfgcs/p/6387515.html
Copyright © 2011-2022 走看看