gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:11:03 25 4
gpt4 key购买 nike

这是我的代码。它在密码验证之前功能完美。它完全忽略我的空测试并直接进行实际验证,但随后拒绝跳过它并不断向我发出无效密码警报。关于如何解决这个问题有什么想法吗?

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;
}

}

最佳答案

您应该修改从中检索数据的js代码。看,

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

这里您试图获取不是输入标签的值,您正在尝试获取标签。所以你应该将上面的代码替换为:

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

希望对你有帮助

关于javascript - 如何在 javascript 中使用正则表达式验证密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30115172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com