密码应为字母数字。
密码的首字母应为大写。
密码必须包含特殊字符(@,$,!,&等)。
密码长度必须大于8个字符。
最重要的密码字段不应为空。
<html>
<head>
<title> Verification of valid Password </title>
</head>
<script>
function verifyPassword() {
var pw = document.getElementById("pswd").value;
// 检查空密码字段
if(pw == "") {
document.getElementById("message").innerHTML = "****请输入密码!";
return false;
}
// 最小密码长度验证
if(pw.length < 8) {
document.getElementById("message").innerHTML = "**密码长度必须至少为8个字符";
return false;
}
// 密码验证的最大长度
if(pw.length > 15) {
document.getElementById("message").innerHTML = "**密码长度不能超过15个字符";
return false;
} else {
alert("Password is correct");
}
}
</script>
<body>
<center>
<p style="color:green">lidihuo</p>
<p> 验证有效密码示例 </p>
<form onsubmit ="return verifyPassword()">
<!-- Enter Password -->
<td> 输入密码 </td>
<input type = "password" id = "pswd" value = "">
<span id = "message" style="color:red"> </span> <br><br>
<!-- 单击以验证有效密码 -->
<input type = "submit" value = "Submit">
<!-- 单击以重置字段 -->
<button type = "reset" value = "Reset" >Reset</button>
</form>
</center>
</body>
</html>
lidihuo
验证有效密码示例
<html>
<head>
<title> Password Matching Validation </title>
</head>
<script>
function matchPassword() {
var pw1 = document.getElementById("pswd1").value;
var pw2 = document.getElementById("pswd2").value;
if(pw1 != pw2)
{
alert("密码不匹配");
} else {
alert("密码创建成功");
}
}
</script>
<body>
<center>
<form>
<h1 style="color:green">lidihuo</h1>
<h3> Confirm password Validation Example </h3>
<!-- Enter Password -->
<td> Enter Password </td>
<input type = "password" name = "pswd1" id="pswd1"> <br><br>
<!-- Enter Confirm password -->
<td> Confirm Password </td>
<input type = "password" name = "pswd2" id="pswd2> <br><br>
<!?Click to validate confirm password -->
<button type = "submit" onclick="matchPassword()">Submit</button>
<!-- Click to reset fields -->
<button type = "reset" value = "Reset" >Reset</button>
</form>
</center>
</body>
</html>
空字段验证
最小密码长度验证,即 > 8
最大密码长度验证,即 < 15
确认密码验证
<html>
<head>
<title> Validate the Password </title>
</head>
<script>
function validateForm() {
//collect form data in JavaScript variables
var pw1 = document.getElementById("pswd1").value;
var pw2 = document.getElementById("pswd2").value;
var name1 = document.getElementById("fname").value;
var name2 = document.getElementById("lname").value;
//check empty first name field
if(name1 == "") {
document.getElementById("blankMsg").innerHTML = "**Fill the first name";
return false;
}
//character data validation
if(!isNaN(name1)){
document.getElementById("blankMsg").innerHTML = "**Only characters are allowed";
return false;
}
//character data validation
if(!isNaN(name2)){
document.getElementById("charMsg").innerHTML = "**Only characters are allowed";
return false;
}
//check empty password field
if(pw1 == "") {
document.getElementById("message1").innerHTML = "**Fill the password please!";
return false;
}
//check empty confirm password field
if(pw2 == "") {
document.getElementById("message2").innerHTML = "**Enter the password please!";
return false;
}
//minimum password length validation
if(pw1.length < 8) {
document.getElementById("message1").innerHTML = "**Password length must be atleast 8 characters";
return false;
}
//maximum length of password validation
if(pw1.length > 15) {
document.getElementById("message1").innerHTML = "**Password length must not exceed 15 characters";
return false;
}
if(pw1 != pw2) {
document.getElementById("message2").innerHTML = "**Passwords are not same";
return false;
} else {
alert ("Your password created successfully");
document.write("JavaScript form has been submitted successfully");
}
}
</script>
<body>
<h1 style="color:green">lidihuo</h1>
<h3> Verify valid password Example </h3>
<form onsubmit ="return validateForm()">
<!-- Enter first name -->
<td> Full Name* </td>
<input type = "text" id = "fname" value = "">
<span id = "blankMsg" style="color:red"> </span> <br><br>
<!-- Enter last name -->
<td> Last Name </td>
<input type = "text" id = "lname" value = "">
<span id = "charMsg" style="color:red"> </span> <br><br>
<!-- Create a new password -->
<td> Create Password* </td>
<input type = "password" id = "pswd1" value = "">
<span id = "message1" style="color:red"> </span> <br><br>
<!?Enter confirm password -->
<td> Confirm Password* </td>
<input type = "password" id = "pswd2" value = "">
<span id = "message2" style="color:red"> </span> <br><br>
<!-- Click to verify valid password -->
<input type = "submit" value = "Submit">
<!-- Click to reset fields -->
<button type = "reset" value = "Reset" >Reset</button>
</form>
</body>
</html>
lidihuo
Verify valid password Example