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

时间:2021-06-22 15:44:41

This is my code. It functions perfectly up to the password validation. It completely ignores my null test and goes straight to the actual validation but then refuses to go past it and keeps giving me my invalid password alert. Any ideas as to how to fix this issue?

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

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 个解决方案

#1


You should amend js code that retriving data from from. Look,

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

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

Here You are trying to get NOT values of input tags, you are trying to get tag. So You should replace above code with:

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

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

I hope it will help you

我希望它会对你有所帮助

#2


You should pass the value of password fields.

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

try changing the code to

尝试将代码更改为

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

#1


You should amend js code that retriving data from from. Look,

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

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

Here You are trying to get NOT values of input tags, you are trying to get tag. So You should replace above code with:

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

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

I hope it will help you

我希望它会对你有所帮助

#2


You should pass the value of password fields.

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

try changing the code to

尝试将代码更改为

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