我似乎无法让这段代码工作。有什么建议么?此表单应返回 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>