简体   繁体   中英

How to make validation for EditText in android?

I am developing an app in which I have some edit Text and some validation on them but when I enter "JJJJ@GMAil.cOM" it except and also for "JJJJ@JJj.com". how do I do validation for that here is code:-

public boolean validation(String mobile, String pass) {

    if (mobile != null && mobile.length() > 7 && mobile.length() < 15) {
        if (pass.length() >= 4 && pass.length() <= 8) {
            return true;
        } else {
            m_InputPassword.setError("Password must be between 4 to 8 characters long");
            return false;
        }
    } else {
        m_InputMobile.setError("Mobile number must be between 7 to 15 characters long");
        return false;
    }
}   

private boolean isValidEmail(String email) {

    String regExpn = "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@" + "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?" + "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
            + "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?" + "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" + "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";
    CharSequence inputStr = email;
    Pattern pattern = Pattern.compile(regExpn, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches())
        return true;
    else
        return false;
}

您可以为每个关联的EditText创建一个布尔字段,因此现在您可以进行验证,如果出现错误,则将此布尔值之一设置为true ,那么您只需要为每个EditText设置一个OnClickListener() ,如果其中之一将其关联的布尔值设置为true,在侦听器中为其设置错误。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM