EditText上的验证无法正常工作

时间:2016-05-13 06:34:01

标签: java android validation

我有注册活动并尝试验证注册按钮上的字段点击。我使用setError()方法显示错误消息它的工作但是存在问题它正在处理当时关注的特定字段。我想要的是它应该检查所有字段并验证消息是否应该一次出现在所有字段 OR 如果多于1个字段无效,则从上到下依次为1

到目前为止我所拥有的是什么 注册按钮代码

if (mSignUpBtn != null) {
            mSignUpBtn.setOnClickListener(new View.OnClickListener() {
                @Override


           public void onClick(View arg0) {

                assert unameEditText != null;
                final String uName = unameEditText.getText().toString();
                if (!isValidName(uName)) {
                    unameEditText.setVisibility(View.VISIBLE);
                    unameEditText.setError("Invalid Username, Username must be A-Z,a-z,_,0-9 Can't contain Space");
                }
                assert emailEditText != null;
                final String email = emailEditText.getText().toString();
                if (!isValidEmail(email)) {
                    unameEditText.setVisibility(View.VISIBLE);
                    emailEditText.setError("Invalid Email");
                }
                assert passEditText != null;
                final String pass = passEditText.getText().toString();
                if (!isValidPassword(pass)) {
                    unameEditText.setVisibility(View.VISIBLE);
                    passEditText.setError("Invalid Password");
                }
            }
        });
    }

验证方法代码

private boolean isValidName(String uName) {
    String NAME_PATTERN = "[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789_]*";

    Pattern pattern = Pattern.compile(NAME_PATTERN);
    Matcher matcher = pattern.matcher(uName);
    return matcher.matches();
}

// validating email id
private boolean isValidEmail(String email) {
    String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    Pattern pattern = Pattern.compile(EMAIL_PATTERN);
    Matcher matcher = pattern.matcher(email);
    return matcher.matches();
}

// validating password with retype password
private boolean isValidPassword(String pass) {
    return ( pass.length() > 6);
}

一切都很好,除非它应该按顺序验证所有字段 OR 而不仅仅是聚焦

这是一张图片,显示USERNAME字段是否包含空格,显示错误消息但未在电子邮件和密码字段中显示任何内容,因为这些字段不在焦点

enter image description here

我应该怎么做才能修复它?感谢

2 个答案:

答案 0 :(得分:1)

试试这个;

public void onClick(View arg0) {

         assert unameEditText != null;
         final String uName = unameEditText.getText().toString();
         assert emailEditText != null;
         final String email = emailEditText.getText().toString();
         assert passEditText != null;
         final String pass = passEditText.getText().toString();
         if (!isValidName(uName)) {
             unameEditText.setVisibility(View.VISIBLE);
             unameEditText.setError("Invalid Username, Username must be A-Z,a-z,_,0-9 Can't contain Space");
         }else if (!isValidEmail(email)) {
             unameEditText.setVisibility(View.VISIBLE);
             emailEditText.setError("Invalid Email");
         }else if (!isValidPassword(pass)) {
             unameEditText.setVisibility(View.VISIBLE);
             passEditText.setError("Invalid Password");
         }
     }

答案 1 :(得分:0)

拿一个布尔标志并检查它以保持秩序,如下所示,它首先检查用户名而不是电子邮件,然后传递

boolean isSave = true;

if (mSignUpBtn != null) {
            mSignUpBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {

                assert unameEditText != null;
                final String uName = unameEditText.getText().toString();
                if (!isValidName(uName)) {
                    unameEditText.setVisibility(View.VISIBLE);
                    unameEditText.setError("Invalid Username, Username must be A-Z,a-z,_,0-9 Can't contain Space");
                    isSave = false;
                }
                assert emailEditText != null;
                final String email = emailEditText.getText().toString();
                if (isSave && !isValidEmail(email)) {
                    unameEditText.setVisibility(View.VISIBLE);
                    emailEditText.setError("Invalid Email");
                    isSave = false;
                }
                assert passEditText != null;
                final String pass = passEditText.getText().toString();
                if (isSave && !isValidPassword(pass)) {
                    unameEditText.setVisibility(View.VISIBLE);
                    passEditText.setError("Invalid Password");
                    isSave = false;
                }
            }
        });
    }