如何在javascript中使用正则表达式验证密码?

时间:2015-05-08 03:17:08

标签: javascript regex validation

这是我的代码。它完全适用于密码验证。它完全忽略了我的空测试并直接进行实际验证,但后来拒绝通过它并继续给我无效的密码提示。关于如何解决这个问题的任何想法?

function validate(){

    var l = document.getElementById('lname').value;
    var f = document.getElementById('fname').value;
    var e = document.getElementById('email').value;
    var e2 = document.getElementById('cemail').value;
    var u = document.getElementById('newuser').value;
    var p = document.getElementById('newpass');
    var p2 = document.getElementById('cnewpass');

    var str = new RegExp(/[a-zA-Z]{1,30}$/);
    var em = new RegExp(/[a-z0-9._-]+@[a-z]+\.[a-z]{1,30}$/);
    var pass = new RegExp(/[a-zA-Z0-9]{1,15}$/);    

    if (l == null || l == ""){
        alert("Please enter your last name");
        return false;
    }
    var ln = str.test(l);   
    if(ln==false){
        alert("Invalid Name.");
        documents.forms['registration']['lname'].focus;
        return false;
    }

    if (f == null || f == ""){
        alert("Please enter your first name");
        return false;
    }
    var fn = str.test(f);
    if(fn==false){
        alert("Invalid Name.");
        documents.forms['registration']['fname'].focus;
        return false;
    }

    if (e == null || e == "") {
        alert("Please enter an email address");
        return false;
    }
    if (e2 == null || e2 == "") {
        alert("Please enter an email address");
        return false;
    }
    var eml = em.test(e);
    if(eml==false){
        alert("Invalid Email.");
        documents.forms['registration']['email'].focus;
        return false;
    }
    var eml2 = em.test(e2);
    if(eml2==false){
        alert("Invalid Email.");
        documents.forms['registration']['cemail'].focus;    
        return false;
    }   
    if(e2!=e){
        alert("Please ensure that the emails match.");
        return false;
    }

    if (u == null || u == "") {
        alert("Please enter a user name");
        return false;
    }
    var un = str.test(u);   
    if(un==false){
        alert("Invalid user name");
        documents.forms['registration']['newuser'].focus;
        return false;
    }   

    if (p == null || p == "") {
        alert("works");
        alert("Please enter a password");
        return false;
    }
    if (p2 == null || p2 == "") {
        alert("Please enter a password");
        return false;
    }   
    var pwrd = pass.test(p);    
    if(pwrd==false){
        alert("Invalid Password.");
        documents.forms['registration']['newpass'].focus;
        return false;
    }

    if(p2!=p){
        alert("Please ensure that the passwords match");
        documents.forms['registration']['cnewpass'].focus;
        return false;
    }

}

2 个答案:

答案 0 :(得分:1)

你应该修改从中回收数据的js代码。看,

var p = document.getElementById('newpass');
var p2 = document.getElementById('cnewpass');

这里你试图得到输入标签的NOT值,你试图得到标签。 所以你应该用以下代码替换上面的代码:

var p = document.getElementById('newpass').value;
var p2 = document.getElementById('cnewpass').value;

我希望它能帮到你

答案 1 :(得分:0)

您应该传递密码字段的值。

尝试将代码更改为

    var p = document.getElementById('newpass').value;
    var p2 = document.getElementById('cnewpass').value;