iOS 正则表达式判断UITextField是否为全汉字,全字母,全数字,数字和字母
判断全汉字(这个问题苦恼了我很久,现在很方便,几行代码就搞定):
if ([self deptNameInputShouldChinese]) {
[DemonAlertHelper showToastWithMessage:@"只能是中文"];
return;
}
调用这个方法就可
pragma mark--
pragma mark 输入中文
(BOOL) deptNameInputShouldChinese
{
NSString *regex = @"[\u4e00-\u9fa5]+";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
if (![pred evaluateWithObject:inputTextField.text]) {
return YES;
}
return NO;
}
判断全数字:(BOOL) deptNumInputShouldNumber
{
NSString regex =@"[0-9]";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
if (![pred evaluateWithObject:inputTextField.text]) {
return YES;
}
return NO;
}
判断全字母:(BOOL) deptPassInputShouldAlpha
{
NSString regex =@"[a-zA-Z]";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
if (![pred evaluateWithObject:inputTextField.text]) {
return YES;
}
return NO;
}
判断仅输入字母或数字:(BOOL) deptIdInputShouldAlphaNum
{
NSString regex =@"[a-zA-Z0-9]";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
if (![pred evaluateWithObject:inputTextField.text]) {
return YES;
}
return NO;
}