作者:SIX道仙人 | 来源:互联网 | 2023-09-17 12:49
一.ASCII码来判断先附上ASCII码表:在附上OC代码:这里只做了限制数字,大小写字母以外的输入,可根据实际情况做调整***Description0-9A-Za-z**@par
一.ASCII码来判断先附上ASCII码表:
在附上OC代码:这里只做了限制 数字,大小写字母以外的输入,可根据实际情况做调整
/**
* Description 0-9 A-Z a-z
*
* @param string 输入的字符串 键盘输入时点击提示字符将多余2个以上的长度
*
* @return
*/
+ (BOOL)judgeTextFieldInputString:(NSString *)string{
for (int i = 0; i NSString *subString = [string substringWithRange:NSMakeRange(i, 1)];
if ([subString isEqualToString:@" "]) {
continue;
}
int ascii = [subString characterAtIndex:0];
if (![self judgeTextFieldInputCStringASCII:ascii]) {
return NO;
break;
}
}
return YES;
}
+ (BOOL)judgeTextFieldInputCStringASCII:(int)ascii{
int ascii_a = [@"a" characterAtIndex:0];//转ASCII码
int ascii_z = [@"z" characterAtIndex:0];
int ascii_A = [@"A" characterAtIndex:0];
int ascii_Z = [@"Z" characterAtIndex:0];
int ascii_0 = [@"0" characterAtIndex:0];
int ascii_9 = [@"9" characterAtIndex:0];
// int ascii = [string characterAtIndex:0];
if( ascii >= ascii_A && ascii <= ascii_Z){
NSLog(@"A-Z: %d",ascii);
return YES;
}else if( ascii >= ascii_a && ascii <= ascii_z){
NSLog(@"a-z :%d",ascii);
return YES;
}else if( ascii >= ascii_0 && ascii <= ascii_9){
NSLog(@"0-9 :%d",ascii);
return YES;
}
return NO;
}
二.NSScanner字符扫描来判断-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
输入框中只能输入数字和小数点,且小数点只能输入一位,参数number 可以设置小数的位数
return [ConditionJudgmentController isValidAboutInputText:textField shouldChangeCharactersInRange:range replacementString:string decimalNumber:3];
}
+(BOOL)isValidAboutInputText:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string decimalNumber:(NSInteger)number{
NSScanner *scanner = [NSScanner scannerWithString:string];
NSCharacterSet *numbers;
NSRange pointRange = [textField.text rangeOfString:@"."];
if ( (pointRange.length > 0) && (pointRange.location range.location + range.length) ){
numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
}else{
numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
}
if ( [textField.text isEqualToString:@""] && [string isEqualToString:@"."] ){
return NO;
}
short remain = number; //保留 number位小数
NSString *tempStr = [textField.text stringByAppendingString:string];
NSUInteger strlen = [tempStr length];
if(pointRange.length > 0 && pointRange.location > 0){ //判断输入框内是否含有“.”。
if([string isEqualToString:@"."]){ //当输入框内已经含有“.”时,如果再输入“.”则被视为无效。
return NO;
}
if(strlen > 0 && (strlen - pointRange.location) > remain+1){ //当输入框内已经含有“.”,当字符串长度减去小数点前面的字符串长度大于需要要保留的小数点位数,则视当次输入无效。
return NO;
}
}
NSRange zeroRange = [textField.text rangeOfString:@"0"];
if(zeroRange.length == 1 && zeroRange.location == 0){ //判断输入框第一个字符是否为“0”
if(![string isEqualToString:@"0"] && ![string isEqualToString:@"."] && [textField.text length] == 1){ //当输入框只有一个字符并且字符为“0”时,再输入不为“0”或者“.”的字符时,则将此输入替换输入框的这唯一字符。
textField.text = string;
return NO;
}else{
if(pointRange.length == 0 && pointRange.location > 0){ //当输入框第一个字符为“0”时,并且没有“.”字符时,如果当此输入的字符为“0”,则视当此输入无效。
if([string isEqualToString:@"0"]){
return NO;
}
}
}
}
NSString *buffer;
if ( ![scanner scanCharactersFromSet:numbers intoString:&buffer] && ([string length] != 0) ){
return NO;
}else{
return YES;
}
}