一:正则表达式再用 
 
#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; 
} 
 
 

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

iOS实现图片高斯模糊效果详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。