码迷,mamicode.com
首页 > 移动开发 > 详细

iOS常用正则表达式

时间:2016-08-25 14:28:46      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

原文地址:http://blog.csdn.net/h643342713/article/details/52241832

 

1.验证邮箱

+(BOOL)validateEmail:(NSString*)email{

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

    NSPredicate *emailPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailString];

    return [emailPredicate evaluateWithObject:email];

}

 

2.验证手机号

+(BOOL)validatePhonoNum:(NSString *)phono{

    //手机号以13, 15,17,18开头,八个 \d 数字字符

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

    NSPredicate *phonePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneString];

    return [phonePredicate evaluateWithObject:phono];

}

 

3.验证车牌号

+(BOOL)validateCarNum:(NSString* )car{

    NSString *carString = @"^[A-Za-z]{1}[A-Za-z_0-9]{5}$";

    NSPredicate *carPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", carString];

    return [carPredicate evaluateWithObject:car];

}

 

4.用户名  

+ (BOOL)validateUserName:(NSString*)name {  

   NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$"; 

   NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex]; 

   BOOL  peopleName = [userNamePredicate evaluateWithObject:name]; 

   return peopleName;  

 

5.密码  

+ (BOOL)validatePassword:(NSString*)passWord {  

   NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$"; 

    NSPredicate*passWordPredicate = [NSPredicate  predicateWithFormat:@"SELF MATCHES%@",passWordRegex]; 

    return [passWordPredicate evaluateWithObject:passWord]; 

}  

  

  

6.昵称  

+ (BOOL)validateNickname:(NSString*)nickname  {  

   NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$"; 

    NSPredicate*passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",nicknameRegex]; 

    return [passWordPredicate evaluateWithObject:nickname]; 

iOS常用正则表达式

标签:

原文地址:http://www.cnblogs.com/Hakim/p/5806598.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!