iOS-UITextField限制输入字符数(手机号/密码)

iOS每个应用都要做注册和登录这块,我把手机号和密码的验证代码附上。供参考。

iOS-UITextField限制输入字符数(手机号/密码)_第1张图片
69FA5925-88D0-4102-BF4D-937D10B102E2.png

判断一共有两种方法

  • UITextField 的Target事件,因为UITextField继承自UIControl,事件类型为UIControlEventEditingChanged
[verifyNumberTextField  addTarget:self action:@selector(verifyNumberTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  • UITextField的代理方法UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; 

详细代码如下

1、头文件

@interface LoginViewController ()

@property(nonatomic,strong)UITextField *phoneNumberTextField;
@property(nonatomic,strong)UITextField *passwordTextField;

@property(nonatomic,strong)UILabel *phoneText;
@property(nonatomic,strong)UILabel *pwdText;

@property(nonatomic,strong)UIButton *loginBtn;


@end

2、源文件

- (void)initSubViews {
    
    UITextField *phoneNumber = [[UITextField alloc]init];
    phoneNumber.placeholder = @"输入手机号";
    phoneNumber.keyboardType = UIKeyboardTypeNumberPad;
    phoneNumber.clearButtonMode = UITextFieldViewModeWhileEditing;
    [phoneNumber setValue:KRGBColor(79, 79, 79) forKeyPath:@"_placeholderLabel.textColor"];
    [phoneNumber setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    phoneNumber.borderStyle = UITextBorderStyleRoundedRect;
    phoneNumber.delegate = self;
    [phoneNumber addTarget:self action:@selector(phoneNumberTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:phoneNumber];
    self.phoneNumberTextField = phoneNumber;
    [phoneNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(30);
        make.top.mas_equalTo(40);
    }];
    
    UILabel *phoneText = [[UILabel alloc]init];
    phoneText.hidden = YES;
    phoneText.text = @"请输入正确手机号";
    phoneText.font = [UIFont systemFontOfSize:10];
    phoneText.textColor = [UIColor redColor];
    self.phoneText = phoneText;
    [self.view addSubview:phoneText];
    [phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(phoneNumber);
        make.top.mas_equalTo(phoneNumber.mas_bottom);
    }];
    
    
    
    UITextField *password = [[UITextField alloc]init];
    password.placeholder = @"输入密码";
    password.clearButtonMode = UITextFieldViewModeWhileEditing;
    [password setValue:KRGBColor(79, 79, 79) forKeyPath:@"_placeholderLabel.textColor"];
    [password setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    password.borderStyle = UITextBorderStyleRoundedRect;
    password.secureTextEntry = YES;
    [self.view addSubview:password];
    password.delegate = self;
    [password addTarget:self action:@selector(passwordTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.passwordTextField = password;
    [password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(30);
        make.top.mas_equalTo(phoneNumber.mas_bottom).offset(20);
    }];
    
    UILabel *pwdDetailLable = [[UILabel alloc]init];
    pwdDetailLable.text = @"6-16位";
    pwdDetailLable.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:pwdDetailLable];
    [pwdDetailLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(password).offset(2);
        make.top.mas_equalTo(password.mas_bottom);
    }];
    
    UILabel *pwdText = [[UILabel alloc]init];
    pwdText.hidden = YES;
    pwdText.text = @"请输入6-16位密码";
    pwdText.font = [UIFont systemFontOfSize:10];
    pwdText.textColor = [UIColor redColor];
    self.pwdText = pwdText;
    [self.view addSubview:pwdText];
    [pwdText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(password);
        make.top.mas_equalTo(pwdDetailLable.mas_bottom);
    }];
    
    
    
    UIButton *forgetpwdBtn = [[UIButton alloc]init];
    [forgetpwdBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    forgetpwdBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [forgetpwdBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:forgetpwdBtn];
    [forgetpwdBtn addTarget:self action:@selector(forgetpwdBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [forgetpwdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(password.mas_bottom).offset(5);
        make.right.mas_equalTo(password);
    }];
    
    UIButton *loginBtn = [[UIButton alloc]init];
    loginBtn.enabled = NO;
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    loginBtn.layer.cornerRadius = 5;
    loginBtn.layer.masksToBounds = YES;
    loginBtn.backgroundColor = ColorWithBlue;
    [loginBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:loginBtn];
    [loginBtn addTarget:self action:@selector(loginBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.loginBtn = loginBtn;
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(30);
        make.top.mas_equalTo(forgetpwdBtn.mas_bottom).offset(20);
    }];
    
}

#pragma mark- 手机号输入框的事件
- (void)phoneNumberTextFieldDidChange:(UITextField *)textField{
    
    if (textField.text.length == 11) {
        self.phoneText.hidden = YES;
    }
    
    if (textField.text.length > 11) {
        textField.text = [textField.text substringToIndex:11];
    }
    
    if (self.passwordTextField.text.length != 0 &&self.passwordTextField.text.length<6) {
        self.pwdText.hidden = NO;
    }
    
    //验证登录按钮的可用与否
    if (textField.text.length == 11 && self.passwordTextField.text.length > 5) {
        self.loginBtn.enabled = YES;
    }else{
        self.loginBtn.enabled = NO;
    }
}

#pragma mark- 密码输入框的事件
- (void)passwordTextFieldDidChange:(UITextField *)textField{
    
    if (textField.text.length > 5) {
        self.pwdText.hidden = YES;
    }
    
    if (textField.text.length > 16) {
        textField.text = [textField.text substringToIndex:16];
    }
    
    if (self.phoneNumberTextField.text.length != 11) {
        self.phoneText.hidden = NO;
    }
    
    //验证登录按钮的可用与否
    if (self.phoneNumberTextField.text.length == 11 && textField.text.length > 5) {
        self.loginBtn.enabled = YES;
    }else{
        self.loginBtn.enabled = NO;
    }
    
}

@end

第二种代理方法实现

#pragma mark- UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    // 1.当前完整的内容
    NSString *text = textField.text;
    if (range.length == 0) {
        // 当前是添加内容
        text = [text stringByAppendingString:string];
    } else {
        text = [text substringToIndex:text.length - 1];
    }
    // 2.如果当前编辑的是用户名文本
    if (textField == _phoneNumberTextField) {
        if (text.length == 11 &&  _passwordTextField.text.length >= 6) {
            _loginBtn.enabled = YES;
        } else {
            _loginBtn.enabled = NO;
        }
    } else if (textField == _passwordTextField) {
        if (text.length >= 6 &&  _phoneNumberTextField.text.length == 11) {
            _loginBtn.enabled = YES;
        } else {
            _loginBtn.enabled = NO;
        }
    }

    // 3.限制文本长度
    if (textField == _passwordTextField) {
        return range.location <= 16;
    } else {
        return range.location <= 10;
    }


}

你可能感兴趣的:(iOS-UITextField限制输入字符数(手机号/密码))