-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string{
if(textField ==self.searchEdTextField.rightTextField) {
NSUIntegerlengthOfString = string.length;
//lengthOfString的值始终为1
for(NSIntegerloopIndex =0; loopIndex < lengthOfString; loopIndex++) {
unicharcharacter = [stringcharacterAtIndex:loopIndex];
//将输入的值转化为ASCII值(即内部索引值),可以参考ASCII表
// 48-57;{0,9};65-90;{A..Z};97-122:{a..z}
if(character <48)returnNO;// 48 unichar for 0
if(character >57&& character <65)returnNO;//
if(character >90&& character <97)returnNO;
if(character >122)returnNO;
}
// Check for total length
NSUIntegerproposedNewLength = textField.text.length- range.length+ string.length;
if(proposedNewLength >11) {
returnNO;//限制长度
}
returnYES;
}
return YES;
}
网友评论