我正在创建一个密码功能,其中密码必须包含数字,符号以及大写和小写字母。但是,当我尝试输入代码来验证这一点时,似乎没有任何工作。如果他们的密码少于8个字符,如果他们的确认密码与第一个密码输入不匹配,并且他们没有输入确认密码,我有警报工作,让用户知道他们的密码是否少于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>
答案 0 :(得分:1)
如果密码匹配,它将跳过整个其他块:
//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>
答案 1 :(得分:0)
我猜你需要使用正则表达式,如
(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/)
最少八个字符,至少一个字母,一个数字和一个特殊字符:
"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$""^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$"
最少八个字符,至少一个大写字母,一个小写字母,一个数字和一个特殊字符:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}"
最少8个字符和最多10个字符,至少一个大写字母,一个小写字母,一个数字和一个特殊字符:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,10}"