iOS 正则表达式实例

判断 账号 2-16位

+(BOOL)checkAccountInput:(NSString *)_text

{

NSString *Regex = @"^\\w{2,16}$";

NSPredicate *tPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];

if ([tPredicate evaluateWithObject:_text]) {

return YES ;

}

return NO ;

}

判断手机号码,1开头的十一位数字

+(BOOL)checkPhoneNumInput:(NSString *)_text

{

NSString *Regex = @"1\\d{10}";

NSPredicate *tPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];

if ([tPredicate evaluateWithObject:_text]) {

return YES ;

}

return [tPredicate evaluateWithObject:_text];

}

判断邮箱格式

+(BOOL)checkMailInput:(NSString *)_text

{

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

NSPredicate *tPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];

if ([tPredicate evaluateWithObject:_text]) {

return YES ;

}

return NO ;

}

判断密码 6-16位

+(BOOL)checkPasswordInput:(NSString *)_text

{

NSString *Regex = @"\\w{6,16}";

NSPredicate *tPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];

if ([tPredicate evaluateWithObject:_text]) {

return YES ;

}

return NO;

}

判断验证码 6位

+(BOOL)checkCodeInput:(NSString *)_text

{

NSString *Regex = @"\\w{6}";

NSPredicate *tPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];

if ([tPredicate evaluateWithObject:_text]) {

return YES ;

}

return NO;

}

判断身份证 18位

+(BOOL)checkCerticateInput:(NSString *)_text

{

NSString *Regex = @"\\w{18}";

NSPredicate *tPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];

if ([tPredicate evaluateWithObject:_text]) {

return YES ;

}

return NO;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容