- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到輸入框的內(nèi)容
if (_cardIDtextFiled == textField){
//檢測(cè)是否為純數(shù)字
if ([self isPureInt:string]) {
//添加空格掖桦,每4位之后本昏,4組之后不加空格,格式為xxxx xxxx xxxx xxxx xxxxxxxxxxxxxx
if (textField.text.length % 5 == 4 && textField.text.length < 22) {
textField.text = [NSString stringWithFormat:@"%@ ", textField.text];
}
//只要30位數(shù)字
if ([toBeString length] >= 19+4+11)
{
toBeString = [toBeString substringToIndex:19+4+11];
_cardIDtextFiled.text = toBeString;
[_cardIDtextFiled resignFirstResponder];
return NO;
}
}
else if ([string isEqualToString:@""]) { // 刪除字符
if ((textField.text.length - 2) % 5 == 4 && textField.text.length < 22) {
textField.text = [textField.text substringToIndex:textField.text.length - 1];
}
return YES;
}
else{
return NO;
}
return YES;
}
return YES;
}
- (BOOL)isPureInt:(NSString *)string{
NSScanner* scan = [NSScanner scannerWithString:string];
int val;
return [scan scanInt:&val] && [scan isAtEnd];
}