iOS关于判断输入的密码是否合法,价格动态判断合法性

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
一:正则表达式再用

#pragma 正则匹配手机号
+ (BOOL)checkTelNumber:(NSString *) telNumber
{
     NSString *pattern = @^ 1 +[ 3578 ]+\d{ 9 };
     NSPredicate *pred = [NSPredicate predicateWithFormat: @SELF MATCHES %@, pattern];
     BOOL isMatch = [pred evaluateWithObject:telNumber];
     return isMatch;
}
 
 
#pragma 正则匹配用户密码 6 - 18 位数字和字母组合
+ (BOOL)checkPassword:(NSString *) password
{
     NSString *pattern = @^(?![ 0 - 9 ]+$)(?![a-zA-Z]+$)[a-zA-Z0- 9 ]{ 6 , 18 };
     NSPredicate *pred = [NSPredicate predicateWithFormat: @SELF MATCHES %@, pattern];
     BOOL isMatch = [pred evaluateWithObject:password];
     return isMatch;
      
}
 
#pragma 正则匹配用户姓名, 20 位的中文或英文
+ (BOOL)checkUserName : (NSString *) userName
{
     NSString *pattern = @^[a-zA-Z一-龥]{ 1 , 20 };
     NSPredicate *pred = [NSPredicate predicateWithFormat: @SELF MATCHES %@, pattern];
     BOOL isMatch = [pred evaluateWithObject:userName];
     return isMatch;
      
}
 
 
#pragma 正则匹配用户身份证号 15 或 18 位
+ (BOOL)checkUserIdCard: (NSString *) idCard
{
     NSString *pattern = @(^[ 0 - 9 ]{ 15 }$)|([ 0 - 9 ]{ 17 }([ 0 - 9 ]|X)$);
     NSPredicate *pred = [NSPredicate predicateWithFormat: @SELF MATCHES %@, pattern];
     BOOL isMatch = [pred evaluateWithObject:idCard];
     return isMatch;
}
 
#pragma 正则匹员工号, 12 位的数字
+ (BOOL)checkEmployeeNumber : (NSString *) number
{
     NSString *pattern = @^[ 0 - 9 ]{ 12 };
      
     NSPredicate *pred = [NSPredicate predicateWithFormat: @SELF MATCHES %@, pattern];
     BOOL isMatch = [pred evaluateWithObject:number];
     return isMatch;
      
}
 
#pragma 正则匹配URL
+ (BOOL)checkURL : (NSString *) url
{
     NSString *pattern = @^[ 0 -9A-Za-z]{ 1 , 50 };
     NSPredicate *pred = [NSPredicate predicateWithFormat: @SELF MATCHES %@, pattern];
     BOOL isMatch = [pred evaluateWithObject:url];
     return isMatch;
      

}
二 TextFiled的代理方法

//////// 特殊字符的限制输入,价格金额的有效性判断

#define myDotNumbers     @"0123456789.\n"
#define myNumbers          @"0123456789\n"
-(void) createTextFiled {
    textfield1_ = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    textfield1_.delegate = self;
    [self addSubview:textfield1_];

     textfield2_ = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    textfield2_.delegate = self;
    [self addSubview:textfield2_];

     textfield3_ = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    textfield3_.delegate = self;
    [self addSubview:textfield3_];

}

-(void)showMyMessage:(NSString*)aInfo {

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:aInfo delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
    [alertView release];

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSCharacterSet *cs;
    if ([textField isEqual:textfield1_]) {
        cs = [[NSCharacterSet characterSetWithCharactersInString:myNumbers] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if (!basicTest) {
            [self showMyMessage:@"只能输入数字"];
            return NO;
        }
    }
    else if ([textField isEqual:textfield2_]) {
        cs = [[NSCharacterSet characterSetWithCharactersInString:myNumbers] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if (!basicTest) {
            [self showMyMessage:@"只能输入数字"];
            return NO;
        }
    }
    else if ([textField isEqual:textfield3_]) {
        NSUInteger nDotLoc = [textField.text rangeOfString:@"."].location;
        if (NSNotFound == nDotLoc && 0 != range.location) {
            cs = [[NSCharacterSet characterSetWithCharactersInString:myDotNumbers] invertedSet];
        }
        else {
            cs = [[NSCharacterSet characterSetWithCharactersInString:myNumbers] invertedSet];
        }
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if (!basicTest) {

            [self showMyMessage:@"只能输入数字和小数点"];
            return NO;
        }
        if (NSNotFound != nDotLoc && range.location > nDotLoc + 3) {
            [self showMyMessage:@"小数点后最多三位"];
            return NO;
        }
    }
    return YES;
}



标签: idc

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:javascript 对象创建几种方式

下一篇:Java根据百度API获得经纬度,然后根据经纬度在获得城市信息