千家信息网

IOS正则表达式判断输入类型

发表于:2024-11-18 作者:千家信息网编辑
千家信息网最后更新 2024年11月18日,这篇文章将为大家详细讲解有关IOS正则表达式判断输入类型,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。在开发过程中,有时需要对用户输入的类型做判断,最常见是在注册页
千家信息网最后更新 2024年11月18日IOS正则表达式判断输入类型

这篇文章将为大家详细讲解有关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正则表达式判断输入类型"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

0