iOS--控制特殊字符的输入

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string{

    if(range.length==1&& string.length==0) {

        return YES;

    }

if (textField ==self.testTextField){

return [self validateNumberAndChar:textField.text];

}

return YES;

}

//只能输入英文字母或数字

- (BOOL)validateNumberAndChar:(NSString*)str{

    BOOL resBOOL =YES;

    NSCharacterSet *tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"];

    inti =0;

    while(i < str.length) {

        NSString *string = [str substringWithRange:NSMakeRange(i, 1)];

        NSRangerange = [stringrangeOfCharacterFromSet:tmpSet];

        if(range.length==0) {

            resBOOL =NO;

            break;

        }

        i++;

    }

    return resBOOL;

}

重点:self.testTextField要设置delegate;

你可能感兴趣的:(iOS--控制特殊字符的输入)