美文网首页
只能输入数字和字母

只能输入数字和字母

作者: 阶梯 | 来源:发表于2017-12-19 16:35 被阅读13次
    #define kAlphaNum   @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    
    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
      
        NSString *new_text_str = [textField.text stringByReplacingCharactersInRange:range withString:string];//变化后的字符串
        
        if (textField == self.borrowMoneyView.vinTextField) {
            
            //只能输入数字和字符
            NSCharacterSet *cs;
            cs = [[NSCharacterSet characterSetWithCharactersInString:kAlphaNum] invertedSet];
            NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
            BOOL canChange = [string isEqualToString:filtered];
            
            return canChange;
        }
    
    return YES;
    

    相关文章

      网友评论

          本文标题:只能输入数字和字母

          本文链接:https://www.haomeiwen.com/subject/xrhiwxtx.html