2 回答
TA贡献1887条经验 获得超5个赞
function validateForm() {
if (document.myForm.userName.value && document.myForm.password.value && document.myForm.confirmpassword.value) {
if (document.myForm.password.value === document.myForm.confirmpassword.value) {
if (document.myForm.userName.value.length < 3) {
alert("Username length should be atleast 3 and make sure it starts with A-Z");
return false;
} else if (document.myForm.password.value.length < 8) {
alert("Password must be at least 6 characters long.");
return false;
} else {
if (new RegExp(/^[A-Za-z][A-Za-z0-9]+$/).test(document.myForm.userName.value) === false) {
alert('Username begins with a character[a-zA-Z] ');
return false;
} else if (new RegExp(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$@!%&*?])[A-Za-z\d#$@!%&*?]{8, 30}$ /).test(document.myForm.password.value)) {
alert("password is 8 or more characters where at-least 1 uppercase letter And 1 number AND 1 of the special characters (/-+!@#$^&)");
return false;
} else {
alert('Success !');
return true;
}
}
} else {
alert("password & confirm password not match!");
return false;
}
} else {
alert("Please add valid credentials..");
return false;
}
}
<form name="myForm" action="main.html" method="post">
<!-- onSubmit="return validateForm();" -->
<label>User name</label><br />
<input type="text" name="userName" placeholder="userName" />
<br />
<label>Password</label><br />
<input type="password" name="password" placeholder="password" />
<br />
<label>Confirm Password</label><br />
<input type="password" name="confirmpassword" placeholder="confirm password" />
<br />
<input type="button" value="Login" onclick='validateForm();' />
</form>
注意:-我已经添加了您的所有要点,如果出现问题请告诉我!
添加回答
举报