IOS正则表达式判断输入类型(整理)

工作是一种乐趣时,生活是一种享受!工作是一种义务时,生活则是一种苦役。对待平凡的工作要用不平凡的态度去面对。

在开发过程中,有时需要对用户输入的类型做判断,最常见是在注册页面即用户名和密码,代码整理如下:

只能为中文

-(BOOL)onlyInputChineseCharacters:(NSString*)string{
 NSString *zhString = @"[\u4e00-\u9fa5]+";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",zhString];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

只能为数字

- (BOOL)onlyInputTheNumber:(NSString*)string{
 NSString *numString =@"[0-9]*";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",numString];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

只能为小写

- (BOOL)onlyInputLowercaseLetter:(NSString*)string{
 NSString *regex =@"[a-z]*";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

*只能为大写

- (BOOL)onlyInputACapital:(NSString*)string{
 NSString *regex =@"[A-Z]*";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

允许大小写

- (BOOL)InputCapitalAndLowercaseLetter:(NSString*)string{
 NSString *regex =@"[a-zA-Z]*";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

允许含大小写或数字(不限字数)

- (BOOL)inputLettersOrNumbers:(NSString*)string{
 NSString *regex =@"[a-zA-Z0-9]*";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

允许含大小写或数字(限字数)

-(BOOL)inputNumberOrLetters:(NSString*)name {
 NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
 NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
 BOOL inputString = [userNamePredicate evaluateWithObject:name];
 return inputString;
}

允许汉字或数字(不限字数)

- (BOOL)inputChineseOrNumbers:(NSString*)string{
 NSString *regex =@"[\u4e00-\u9fa5]+[0-9]*";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

允许汉字或数字(限字数)

- (BOOL)inputChineseOrNumbersLimit:(NSString*)string{
 NSString *regex =@"[\u4e00-\u9fa5][0-9]{6,20}+$";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

允许汉字,大小写或数字(不限字数)

- (BOOL)inputChineseOrLettersAndNumbersNum:(NSString*)string{
 NSString *regex =@"[\u4e00-\u9fa5]+[A-Za-z0-9]*";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

允许汉字,大小写或数字(限字数)

- (BOOL)inputChineseOrLettersNumberslimit:(NSString*)string{
 NSString *regex =@"[\u4e00-\u9fa5]+[A-Za-z0-9]{6,20}+$";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

以上所述是小编给大家介绍的IOS正则表达式判断输入类型(整理),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

本文IOS正则表达式判断输入类型(整理)到此结束。请一定要有自信,你就是一道风景,没必要在别人风景里面仰视。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
浅析golang 正则表达式

基于xpath选择器、PyQuery、正则表达式的格式清理工具详解

javascript正则表达式 限1-2位整数,或者至多含有两位小数的写法

正则表达式中的 .*? 或 .*+ 的意思

javascript正则表达式标记中/g /i /m的用法,以及实例