- (void)reTextFieldDidChange:(UITextField *)textField{//输入文本时调用
//将输入的小写x转换成大写
textField.text = [textField.text stringByReplacingOccurrencesOfString:@"x" withString:@"X"];
//解决联想输入的问题
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"Xx0123456789"] invertedSet];//里面放允许输入的字符
textField.text = [[textField.text componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:];
if (textField.text.length > 18) {
textField.text = [textField.text substringToIndex:18];
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//设置只能输入字母数字 和 文本框最大长度
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"Xx0123456789"] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered] && (textField.text.length - range.length + string.length) <= 18;
}