时间:2021-05-02
在开发过程中,有时需要对用户输入的类型做判断,最常见是在注册页面即用户名和密码,代码整理如下:
只能为中文
? 1 2 3 4 5 6 -(BOOL)onlyInputChineseCharacters:(NSString*)string{ NSString *zhString = @"[\u4e00-\u9fa5]+"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",zhString]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }只能为数字
? 1 2 3 4 5 6 - (BOOL)onlyInputTheNumber:(NSString*)string{ NSString *numString =@"[0-9]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",numString]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }只能为小写
? 1 2 3 4 5 6 - (BOOL)onlyInputLowercaseLetter:(NSString*)string{ NSString *regex =@"[a-z]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }*只能为大写
? 1 2 3 4 5 6 - (BOOL)onlyInputACapital:(NSString*)string{ NSString *regex =@"[A-Z]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }允许大小写
? 1 2 3 4 5 6 - (BOOL)InputCapitalAndLowercaseLetter:(NSString*)string{ NSString *regex =@"[a-zA-Z]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }允许含大小写或数字(不限字数)
? 1 2 3 4 5 6 - (BOOL)inputLettersOrNumbers:(NSString*)string{ NSString *regex =@"[a-zA-Z0-9]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }允许含大小写或数字(限字数)
? 1 2 3 4 5 6 -(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; }允许汉字或数字(不限字数)
? 1 2 3 4 5 6 - (BOOL)inputChineseOrNumbers:(NSString*)string{ NSString *regex =@"[\u4e00-\u9fa5]+[0-9]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }允许汉字或数字(限字数)
? 1 2 3 4 5 6 - (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; }允许汉字,大小写或数字(不限字数)
? 1 2 3 4 5 6 - (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; }允许汉字,大小写或数字(限字数)
? 1 2 3 4 5 6 - (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正则表达式判断输入类型(整理),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了JS正则表达式封装与使用操作。分享给大家供大家参考,具体如下:对一些简单的输入进行正则判断的整理,待优化正则表达式封装//邮箱varemailRe
了解RegExp类型:ECMAScript通过RegExp类型来支持正则表达式。varexpression=/pattern/flags;正则表达式的模式(pa
胡开明用正则表达式控制价格输入用正则表达式控制价格输入varrecord={num:""}varcheckDecimal=function(n){vardeci
1、正则表达式包括两部分①定义正则表达式的规则;②正则表达式的模式(i/g/m);2、声明正则表达式①字面量声明:varreg=/表达式规则/表达式模式; e
相关阅读:IOS开发常用的正则表达式Java正则表达式过滤出字母、数字和中文正则表达式(regularexpression)描述了一种字符串匹配的模式,可以用来