gpt4 book ai didi

javascript - JavaScript 表单中的密码验证

转载 作者:可可西里 更新时间:2023-11-01 13:44:07 26 4
gpt4 key购买 nike

我正在创建一个密码函数,其中密码必须包含数字、符号以及大小写字母。但是,当我尝试输入代码来验证这一点时,似乎没有任何效果。如果他们的密码少于 8 个字符,如果他们的确认密码与输入的第一个密码不匹配,以及他们是否没有输入确认密码,我会发出警报。我希望保持同样的风格。

我有一段代码显示不起作用的小写字母代码。我尝试了不同的作品,但实际上似乎没有任何效果。

window.onload = function() {

var subButton = document.getElementById("submit");
subButton.onclick = function value(passForm) {

}

};



function value(passForm) {

/** This function is being used to find out the values input by the user in both the password and confirm password text boxes.
* The results are fed back to the user using alerts.
* **/

//confirm passwords match and have been created
if ((passForm.passInput.value) == (passForm.confirmPassInput.value)) {
alert("Your password has been created!");

} else {

var lower = /(?=.*[a-z])/;
if (!lower(passForm.passInput.value)) {
alert("Password must contain at least one lower case letter.");
passForm.passInput.focus();
return false;
}
else
//Validating length
if ((passForm.passInput.value).length < 8) {
alert("Your password has less than 8 characters.");
passForm.passInput.focus();
return false;
}

else
//Validating confirmation input
if (passForm.confirmPassInput.value == "") {
alert("Please confirm your password.");
passForm.passInput.focus();
return false;
}
else
//Validationg confirmation matches
if (passForm.confirmPassInput.value != passForm.passInput.value) {
alert("Your confirmation password does not match.");
passForm.passInput.focus();
return false;
}

return true;

}
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/security.js"></script>
<title>E-Commerce Security Features</title>
</head>
<body>
<heading>
<h1>E-Commerce Security Practices</h1>
<p id="heading"></p>
</heading>

<h3> Welcome to the E-Commerce Security Practices page! Here you will complete a few <i>simple</i> website security procedures and then decide
what practice worked best and easiest for you. </br> Have fun!</h3>
<form name="passForm" method="post" onsubmit="return value (this)">
<p>In the section below, you are asked to create a username and a password in order to 'login'. </br> Your password <b>must</b> include 8 or more
characters, an upper and lower case letter and a number and a symbol. If your password does not include any of these requirements, it will not be accepted.</p>
Your Password: <input type="password" name="passInput" placeholder="Password" />
</br>
Confirm Password: <input type="password" name="confirmPassInput" placeholder="Re-Enter Password"/>
<input type="submit" value="Submit" id="submit" onclick="return value (passForm) ;"/>
</form>
</body>
</html>

最佳答案

如果密码匹配,它将跳过整个 else block :

 //confirm passwords match and have been created
if ((passForm.passInput.value) == (passForm.confirmPassInput.value)) {
alert("Your password has been created!");

} else {
//you are doing validation here.

您需要运行多个检查:

window.onload = function() {

var subButton = document.getElementById("submit");
subButton.onclick = function value(passForm) {

}

};



function value(passForm) {

/** This function is being used to find out the values input by the user in both the password and confirm password text boxes.
* The results are fed back to the user using alerts.
* **/

//check for lower case
if (!passForm.passInput.value.match(/[a-z]/)) {
alert("Password must contain at least one lower case letter.");
passForm.passInput.focus();
return false;
}

//Validating length
if ((passForm.passInput.value).length < 8) {
alert("Your password has less than 8 characters.");
passForm.passInput.focus();
return false;
}

//Validationg confirmation matches
if (passForm.confirmPassInput.value != passForm.passInput.value) {
alert("Your confirmation password does not match.");
passForm.passInput.focus();
return false;
}

//Validating confirmation input
if (passForm.confirmPassInput.value == "") {
alert("Please confirm your password.");
passForm.passInput.focus();
return false;
}

//check for upper ase
if (!passForm.passInput.value.match(/[A-Z]/)) {
alert("Password must contain at least one upper case letter.");
passForm.passInput.focus();
return false;
}

//check for number
if (!passForm.passInput.value.match(/\d+/g)) {
alert("Password must contain at least one number.");
passForm.passInput.focus();
return false;
}


//confirm passwords match and have been created
if ((passForm.passInput.value) == (passForm.confirmPassInput.value)) {
alert("Your password has been created!");
return true;
}


};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/security.js"></script>
<title>E-Commerce Security Features</title>
</head>
<body>
<heading>
<h1>E-Commerce Security Practices</h1>
<p id="heading"></p>
</heading>

<h3> Welcome to the E-Commerce Security Practices page! Here you will complete a few <i>simple</i> website security procedures and then decide
what practice worked best and easiest for you. </br> Have fun!</h3>
<form name="passForm" method="post" onsubmit="return value (this)">
<p>In the section below, you are asked to create a username and a password in order to 'login'. </br> Your password <b>must</b> include 8 or more
characters, an upper and lower case letter and a number and a symbol. If your password does not include any of these requirements, it will not be accepted.</p>
Your Password: <input type="password" name="passInput" placeholder="Password" />
</br>
Confirm Password: <input type="password" name="confirmPassInput" placeholder="Re-Enter Password"/>
<input type="submit" value="Submit" id="submit" onclick="return value (passForm) ;"/>
</form>
</body>
</html>

关于javascript - JavaScript 表单中的密码验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48613363/

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