添加密码验证后表单验证不工作
Form validation not working after adding password validation
我正在学习JavaScript
,并致力于form validation
。我有一个简单的表单,它要求填写几个不同的字段。
我有我的表单验证工作。然后我添加了一个函数来检查密码字段是否匹配。这也会在用户输入时进行检查。第二个函数工作得很好,但现在我的表单验证的其余部分都停止工作了。
如果你能帮我的话,我将不胜感激。
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<!--[if lt IE 9]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<title>Marty the Robot | About</title>
<link rel="stylesheet" type="text/css" href="css/source.css">
<link rel="stylesheet" type="text/css" href="css/form.css">
<link href='https://fonts.googleapis.com/css?family=Raleway:400,500,300,900' rel='stylesheet' type='text/css'>
<script src="js/script.js" type="text/javascript"></script>
<script src="https://code.jquery.com/jquery-1.7.1.min.js""></script>
</head>
<body>
<main>
<div class="signupform-container">
<div id="form-title">
<h2>
Create an account
</h2>
</div>
<form method="post" action="" id="submitForm">
<div id="validation"></div>
<div id="field1-container" class="field-container">
<label>Name<br>
<div class="space">
</div>
<input type="text" name="FName" placeholder="enter your first name"></label>
</div>
<div id="field2-container" class="field-container">
<label>Last Name<br>
<div class="space">
</div>
<input type="text" name="LName" placeholder="enter your last name"></label>
</div>
<div id="field3-container" class="field-container">
<label>Email<br>
<div class="space">
</div>
<input type="text" name="Email" placeholder="email@email.com"></label>
</div>
<div id="field4-container" class="field-container">
<label>Username<br>
<div class="space">
</div>
<input type="text" name="Username" placeholder="enter a username"></label>
</div>
<div id="field5-container" class="field-container">
<label>Password<br>
<input type="password" id="txtNewPassword" placeholder="enter a password" />
</div>
<div id="field6-container" class="field-container">
<label>Password<br>
<input type="password" id="txtConfirmPassword" placeholder="enter your password again" onChange="checkPasswordMatch();" />
</div>
<div id="field7-container" class="field-container">
<div class="registrationFormAlert" id="divCheckPasswordMatch">
</div>
<div class="registrationFormAlert" id="divCheckPasswordMatch">
<div id="form-submit">
<input type="submit" value="Make it so">
</div>
</form>
<script>
function Validate(form) {
var errors = "";
if (form.Name.value.length === 0) {
form.Name.style.border = "1px solid red";
form.Name.style.backgroundColor = "#FFCCCC";
errors += "<li>Please enter your first name</li>";
}
if (form.LName.value.length === 0) {
form.LName.style.border = "1px solid red";
form.LName.style.backgroundColor = "#FFCCCC";
errors += "<li>Please enter your last name</li>";
}
if (form.Username.value.length === 0) {
form.Username.style.border = "1px solid red";
form.Username.style.backgroundColor = "#FFCCCC";
errors += "<li>Please enter a username</li>";
}
if (form.Email.value.length === 0) {
form.Email.style.border = "1px solid red";
form.Email.style.backgroundColor = "#FFCCCC";
errors += "<li>Please enter an email address</li>";
}
if (form.Password1.value.length <= 4) {
form.Password1.style.border = "1px solid red";
form.Password1.style.backgroundColor = "#FFCCCC";
errors += "<li>Your password is not long enough</li>";
}
if (errors.length > 0) {
document.getElementById("validation").innerHTML = "<ul>" + errors + "</ul>";
return false;
}
return true;
}
document.getElementById("submitForm").onsubmit = function () {
return Validate(this);
};
</script>
<script>
function checkPasswordMatch() {
var password = $("#password").val();
var confirmPassword = $("#confirmpassword").val();
if (password !== confirmPassword)
$("#divCheckPasswordMatch").html("Passwords do not match!");
else
$("#divCheckPasswordMatch").html("Passwords match.");
}
$(document).ready(function () {
$("#txtConfirmPassword").keyup(checkPasswordMatch);
});
</script>
</div>
</main>
</body>
</html>
验证代码
<script>
function Validate(form) {
var errors = "";
if (form.FName.value.length === 0) {
form.FName.style.border = "1px solid red";
form.FName.style.backgroundColor = "#FFCCCC";
errors += "<li>Please enter your first name</li>";
}
if (form.LName.value.length === 0) {
form.LName.style.border = "1px solid red";
form.LName.style.backgroundColor = "#FFCCCC";
errors += "<li>Please enter your last name</li>";
}
if (form.Username.value.length === 0) {
form.Username.style.border = "1px solid red";
form.Username.style.backgroundColor = "#FFCCCC";
errors += "<li>Please enter a username</li>";
}
if (form.Email.value.length === 0) {
form.Email.style.border = "1px solid red";
form.Email.style.backgroundColor = "#FFCCCC";
errors += "<li>Please enter an email address</li>";
}
if (form.Password1.value.length <= 4) {
form.Password1.style.border = "1px solid red";
form.Password1.style.backgroundColor = "#FFCCCC";
errors += "<li>Your password is not long enough</li>";
}
if (errors.length > 0) {
document.getElementById("validation").innerHTML = "<ul>" + errors + "</ul>";
return false;
}
return true;
}
document.getElementById("submitForm").onsubmit = function () {
return Validate(this);
};
</script>
<script>
function checkPasswordMatch() {
var password = $("#password").val();
var confirmPassword = $("#confirmpassword").val();
if (password !== confirmPassword)
$("#divCheckPasswordMatch").html("Passwords do not match!");
else
$("#divCheckPasswordMatch").html("Passwords match.");
}
$(document).ready(function () {
$("#txtConfirmPassword").keyup(checkPasswordMatch);
});
</script>
https://jsfiddle.net/martylavender/n0tb7n4e/你这里打错字了
form.Name.value.length
, 是输入与 name ="name"
改成:
form.FName.value.length
:
if (form.FName.value.length === 0) {
form.FName.style.border = "1px solid red";
form.FName.style.backgroundColor = "#FFCCCC";
errors += "<li>Please enter your first name</li>";
}
。
看起来这是一个问题,我的验证密码字段超过X个字符,我的单独的函数是验证password1和password2匹配。
在测试期间,我删除了密码最小长度的验证,其余的表单验证与密码匹配函数一起工作。
有趣。
相关文章:
- 表单验证工作不正常,在不检查条目的情况下继续
- 尝试在angularJS中获得基本的身份验证工作
- 为什么这个jQuery验证工作错误
- 淘汰日期验证工作不正常
- 表单电子邮件验证工作不正常
- 字段验证工作不正常
- 表单验证工作不正常
- jquery编号验证工作不一致
- 为什么验证工作在表单名而不是模型上
- HTML表单使用JS验证电子邮件,验证工作,但表单仍然提交
- 无法使JavaScript密码正则表达式验证工作
- 我怎么能得到表单验证工作时,使用"共享"按钮(而不是“submit”类型的输入元素)
- Javascript/Jquery -不能让日期选择器+选择验证工作
- Parsley.js 2.0.2要求不工作,但其他验证工作
- 我似乎不能让我的Javascript表单验证工作
- 错误放置不允许jquery验证工作
- 如何让Aurelia验证工作
- 直到JS重定向,Laravel ajax身份验证工作
- 跨域ajax调用windows身份验证工作在chrome和不工作在Firefox
- 我无法让客户端和服务器端验证工作