网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2024/11/16 浏览:3 次 )

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

只能为中文

-(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正则表达式判断输入类型(整理),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

上一篇:JS基础教程——正则表达式示例(推荐)
下一篇:详解基于Linux下正则表达式(基本正则和扩展正则命令使用实例)
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。