这次给大家带来正则怎样在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; }
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上是正则怎样在ios里判断是否输入的详细内容。更多信息请关注PHP中文网其他相关文章!