gpt4 book ai didi

javascript - 更正错误后隐藏 javascript insideHTML

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

我使用了以下 JavaScript 代码在我的表单中添加内联错误。我希望在纠正每个字段后纠正错误后删除错误。我为所有验证创建了一个函数,因此无法使用 else 并手动删除错误。

var emailpattern = /^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,4})$/

var passwordpattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}$/;

function validateForm() {
var x = document.forms["LoginForm"]["email"];
if (x.value == "") {
x.value = "";
document.getElementById('pointemail').innerHTML = "Please enter the email id.";
x.focus();
return false;
} else if (!emailpattern.test(x.value)) {
x.value = "";
document.getElementById('pointemail').innerHTML = "Please enter a valid email address.";
x.focus();
return false;
}
x = document.forms["LoginForm"]["password"];
if (x.value == "") {
x.value = "";
document.getElementById('pointpassword').innerHTML = "Please enter the password.";
x.focus();
return false;
} else if (!passwordpattern.test(x.value)) {
x.value = "";
document.getElementById('pointpassword').innerHTML = "Password should have minimum 8 characters, one upper case, one lower case and one special character";
x.focus();
return false;
}
}

最佳答案

试试这个

var emailpattern = /^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,4})$/

var passwordpattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}$/;

function validateForm() {
var x = document.forms["LoginForm"]["email"];
document.getElementById('pointemail').innerHTML = "";
document.getElementById('pointpassword').innerHTML = "";

//Add your if else here.
}

希望它会有用。 :)

关于javascript - 更正错误后隐藏 javascript insideHTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40080017/

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