如何判断IOS正则表达式的输入类型
来源:爱站网时间:2021-06-22编辑:网友分享
在开发的过程当中,有时候是需要对用户输入的类型去做一些判断,比较常见的就是在注册页面即用户名和密码,那要如何判断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正则表达式的输入类型的文章,如果大家还有不懂的可以关注爱站技术频道小编,后续还会有更多正则表达式相关内容及教程,在此非常感谢朋友们一直以来的支持。
上一篇:正则表达式\w元字符的使用说明