我有一个使用javascript输入验证的表单。我还想在表单中添加密码输入。但现在的问题是我最终得到了两个提交表单,其中一个没有用。然后我尝试将我的javascript验证代码添加到表单输入的现有验证代码中。但是,当我进行测试时,它也无法正常工作。有人可以指出我的代码有什么问题吗?这里是: 这是JavaScript代码:
<script language="javascript">
function check() {
verify_code = document.getElementById("verify_code");
code = document.getElementById("code");
x = document.form1;
if (x.name.value=="") {
alert ("Please enter your Full Name!");
document.form1.name.focus ( )
return false;
}
else if (x.applicant_type.value=="") {
alert("Please select Applicant Type");
document.form1.applicant_type.focus ( )
return false;
}
else if(form1.pwd.value != "" && form1.pwd.value == form1.pwd1.value) {
if(form1.pwd.value.length < 6) {
alert("Error: Password must contain at least six characters!");
form1.pwd.focus();
return false;
}
re = /[0-9]/;
if(!re.test(form1.pwd.value)) {
alert("Error: password must contain at least one number (0-9)!");
form1.pwd.focus();
return false;
}
re = /[a-z]/;
if(!re.test(form1.pwd.value)) {
alert("Error: password must contain at least one lowercase letter (a-z)!");
form1.pwd.focus();
return false;
}
re = /[A-Z]/;
if(!re.test(form1.pwd.value)) {
alert("Error: password must contain at least one uppercase letter (A-Z)!");
form1.pwd.focus();
return false;
}
} else {
alert("Error: Please check that you've entered and confirmed your password!");
form1.pwd.focus();
return false;
}
alert("You entered a valid password: " + form1.pwd.value);
}
else
{ return true; }
}
</script>
这是我的密码输入代码:
<br>Password:<span class="style4">*</span> <input type="password" name="pwd"><br>
<br>Confirm Password:<span class="style4">*</span> <input type="password" name="pwd1"><br>
<span class="formnotes">(For future use if you need to modify your application) </span>
如果我使用此代码,我的密码输入验证将起作用: 使用Javascript:
<script type="text/javascript">
function checkForm(form)
{
if(form.username.value == "") {
alert("Error: Username cannot be blank!");
form.username.focus();
return false;
}
re = /^\w+$/;
if(!re.test(form.username.value)) {
alert("Error: Username must contain only letters, numbers and underscores!");
form.username.focus();
return false;
}
if(form.pwd1.value != "" && form.pwd1.value == form.pwd2.value) {
if(form.pwd1.value.length < 6) {
alert("Error: Password must contain at least six characters!");
form.pwd1.focus();
return false;
}
if(form.pwd1.value == form.username.value) {
alert("Error: Password must be different from Username!");
form.pwd1.focus();
return false;
}
re = /[0-9]/;
if(!re.test(form.pwd1.value)) {
alert("Error: password must contain at least one number (0-9)!");
form.pwd1.focus();
return false;
}
re = /[a-z]/;
if(!re.test(form.pwd1.value)) {
alert("Error: password must contain at least one lowercase letter (a-z)!");
form.pwd1.focus();
return false;
}
re = /[A-Z]/;
if(!re.test(form.pwd1.value)) {
alert("Error: password must contain at least one uppercase letter (A-Z)!");
form.pwd1.focus();
return false;
}
} else {
alert("Error: Please check that you've entered and confirmed your password!");
form.pwd1.focus();
return false;
}
alert("You entered a valid password: " + form.pwd1.value);
return true;
}
</script>
和HTML:
<form ... onsubmit="return checkForm(this);">
<p>Username: <input type="text" name="username"></p>
<p>Password: <input type="password" name="pwd1"></p>
<p>Confirm Password: <input type="password" name="pwd2"></p>
<p><input type="submit"></p>
</form>
但是,当我使用此代码onSubmit="return check();"
时,它无效