摘要: 限制UITextfield的內容應用至關普遍 純數字、字母 、 漢字 的判斷lua
判斷全漢字(這個問題苦惱了我好久,如今很方便,幾行代碼就搞定):spa
if ([self deptNameInputShouldChinese]) {3d
[DemonAlertHelper showToastWithMessage:@"只能是中文"];orm
return;input
}it
調用這個方法就可ast
#pragma mark--方法
#pragma mark 輸入中文di
- (BOOL) deptNameInputShouldChineseco
{
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;
}