为什么即使验证器返回false,我的表单仍然会提交?

时间:2013-11-13 01:22:54

标签: javascript forms validation xhtml

我似乎无法使用此代码。有什么建议? 此表单应返回false,因此如果验证失败则不提交。 但是,无论用户是否未通过验证,我的代码都会提交。

<?xml version = "1.0" encoding = "utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Student Feedback Form Validator</title>
<script type="text/javascript">
function userFocus()  
{
var x=document.forms["Login"]["userName"].value;
if (x==null || x=="")
  {
  document.getElementById("userName").focus();
  }
}
function javaScriptValidation()
{
validateUserName();
validatePin();  
validateSelection();      
}
function validateUserName()
{
var letters = /^[A-Za-z0-9]{6,20}$/;
if (!letters.test(userName.value)) 
  {
    alert("Invalid User Name");
    userName.focus();
        return false;
  }
}
function validatePin()
{
var code = document.getElementById("pinCode");
var letters = /^[0-9]{6,20}$/;
if (!letters.test(code)) 
  {
        alert("Invalid Pin");
        return false;
  }
}
</script>
</head>
<body onload="userFocus()">
<form name="Login" method="post" action="" onsubmit="return javaScriptValidation()">
    <table summary="Form">
<tr>
    <td><label for="username">Your user name:</label></td>
    <td><input type="text" id="userName" name="userName" size="15" maxlength="20"/></td>
</tr>
<tr>
    <td><label for="pinCode">Your pin code:</label></td>
    <td><input type="text" id="pinCode" name="userName" size="15" maxlength="20"/></td>
</tr>
</table>
<p>
<input type="submit" value="Login"/>
<input type="reset" value="Reset"/>
</p>
</form>
</body>
</html>

0 个答案:

没有答案