我的登录表单未获得验证。 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.npassword.value == "") {
alert("Error: Username cannot be blank!");
form.npassword.focus();
return false;
}
re = /[A-Z]/;
if(!re.test(form.npassword.value)) {
alert("Error: password must contain at least one uppercase letter (A-Z)!");
form.npassword.focus();
return false;
}
alert("You entered a valid password: " + form.npassword.value);
return true;
}
</script>
这是登录表单
<div class="section section_with_padding" id="contact">
<h2>Change Password</h2>
<div class="half left">
<h4> </h4>
<table width="91%" align='center' cellpadding= "0" cellspacing= "0" class='main_tab'>
<tr>
<td class='login_table' align='left'><form action="changepass.php" method="post" onsubmit="return checkForm(this);>
<table class="login_tab">
<td class='login_table' align='left'><form method='post'>
<tr>
<td colspan="2" class='login_nam' valign='middle'>Change Password</td>
</tr>
<tr>
<td class='login style1'>Username:</td>
<td class='login'><input type='text' name='username'> </td>
</tr>
<tr>
<td class='login style1'>Old Password:</td>
<td class='login'><input type='password' name='opassword'> </td>
</tr>
<tr>
<td class='login style1'>New Password:</td>
<td class='login'><input type='password' name='npassword'> </td>
</tr>
<tr>
<td class='login' colspan='2'><input type='submit' name='submit' value='Change'>
</td>
</tr>
</table>
</form> </td>
</tr>
</table>
</div>
</div>
请一些帮助验证登录页面,如果没有提供任何输入,则会显示错误(它应显示相应的错误消息)。
答案 0 :(得分:1)
看起来您已经创建了一个嵌套表单。请删除第二个。
<form action="changepass.php" method="post" onsubmit="return checkForm(this);> /* Form HTML */
<td class='login_table' align='left'>
<form method='post'> /* Remove this */
希望这有帮助。
和平!的xD