名称上的正则表达式仍然接受数字

时间:2016-10-12 03:32:24

标签: java android regex validation

    boolean valid = true;

   if (Lnamee.isEmpty() || Pattern.compile("^[a-zA-Z]+$").matcher(Lnamee).matches()) {
        Lname.setError("Enter letters only!");

        valid = false;
    }

    if (Mnamee.isEmpty() || Pattern.compile("^[a-zA-Z]+$").matcher(Mnamee).matches()) {
       Mname.setError("Enter letters only!");
        valid = false;
    }

    if (Fnamee.isEmpty() || Pattern.compile("^[a-zA-Z]+$").matcher(Fnamee).matches()) {
       Fname.setError("Enter letters only!");
        valid = false;
    }

   if (Emails.isEmpty() || Pattern.compile("[a-zA-Z0-9]" + "\\@" + "[a-zA-Z]" + "\\." +  "[a-zA-Z]").matcher(Emails).matches()){
        Email.setError("Enter valid e-mail address!");
        valid = false;
    }

    if (Passwords.isEmpty() || Passwords.length() < 8){
       Password.setError("Password must be 8 characters!");
        valid = false;
    }
    return valid;

名称的正则表达式仍然接受不应该的数字。验证模式有问题吗?我试图寻找其他可能的,但它仍接受数字。

2 个答案:

答案 0 :(得分:0)

你所有的if条款都没有错过。

我认为如果陈述应该有!在正则表达式上。

if (Lnamee.isEmpty() || ! Pattern.compile("^[a-zA-Z]+$").matcher(Lnamee).matches()) {

答案 1 :(得分:0)

您的代码正在运行。它不接受数字

if(Lnamee.isEmpty()|| Pattern.compile(&#34; ^ [a-zA-Z] + $&#34;)。matcher(Lnamee).matches()){         Lname.setError(&#34;仅输入字母!&#34;);

    valid = false;
}