如果有任何错误,我不希望我的表单提交。但我仍然希望用户能够单击提交按钮来检查错误。目前它正在提交,然后告诉用户他或她犯了什么错误。由于错误报告直接与提交按钮相关联,因此它也非常快速地显示错误。那我该怎么办?
这是我的Javascript:
function validateUserName()
{
var u = document.forms["NewUser"]["user"].value
var uLength = u.length;
var illegalChars = /\W/; // allow letters, numbers, and underscores
if (u == null || u == "")
{
$("#ErrorUser").text("You Left the Username field Emptyyy");
return false;
}
else if (uLength <4 || uLength > 11)
{
$("#ErrorUser").text("The Username must be between 4 and 11 characters");
return false;
}
else if (illegalChars.test(u))
{
$("#ErrorUser").text("The Username contains illegal charectors men!");
return false;
}
else
{
return true;
}
}
function validatePassword()
{
var p = document.forms["NewUser"]["pwd"].value
var cP = document.forms["NewUser"]["confirmPwd"].value
var pLength = p.length;
if (p == null || p == "")
{
$("#ErrorPassword1").text("You left the password field empty");
return false;
}
else if (pLength < 6 || pLength > 20)
{
$("#ErrorPassword1").text("Your password must be between 6 and 20 characters in length");
return false;
}
else if (p != cP)
{
$("#ErrorPassword1").text("Th passwords do not match!");
return false;
}
else
{
return true;
}
}
function validateEmail()
{
var e = document.forms["NewUser"]["email"].value
var eLength = e.length;
var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
if (eLength == "" || eLength == null)
{
$("#ErrorEmail").text("You left the email field blank!");
return false;
}
else if (e.match(illegalChars))
{
$("#ErrorEmail").text("ILEGAL CHARECTORS DETECTED EXTERMINATE");
return false;
}
else
{
return true;
}
}
function validateFirstName()
{
var f = document.forms["NewUser"]["fName"].value;
var fLength = f.length;
var illegalChars = /\W/;
if(fLength > 20)
{
$("#ErrorFname").text("First Name has a max of 20 characters");
return false;
}
else if (illegalChars.test(f))
{
$("#ErrorFname").text("Numbers,letter and underscores in first name only");
return false;
}
else
{
return true;
}
}
function validateLastName()
{
var l = document.forms["NewUser"]["lName"].value;
var lLength = l.length;
var illegalChars = /\W/;
if(lLength > 100)
{
$("#ErrorLname").text("Last Name has a max of 100 characters");
return false;
}
else if (illegalChars.test(f))
{
$("#ErrorLname").text("Numbers,letter and underscores in last name only");
return false;
}
else
{
return true;
}
}
function validateForm()
{
//call username function
validateUserName();
//call password function
validatePassword();
//call email function
validateEmail();
//call first name function
validateFirstName();
//call first name function
validateLastName();
//perform form input validation and submit data
}
这是我的 HTML:
<table id = "SignUpTable">
<form name = "NewUser" id = "your-form">
<tr>
<td id = "ErrorUser"></td>
<td class = "FieldName">Username:</td>
<td class = "TextField"><input type = "text" name = "user"/></td>
</tr>
<tr>
<td></td>
<td class = "Information"><em>Must be 4-11 characters.<br/>Only numbers, letters and underscores.</em></td>
</tr>
<tr>
<td id = "ErrorEmail"></td>
<td class = "FieldName">Email:</td>
<td class = "TextField"><Input type = "text" name = "email"/></td>
<tr>
<td></td>
<td class = "Information"><em>We need this to verify your account.</em></td>
</tr>
<tr>
<td id = "ErrorPassword1"></td>
<td class = "FieldName">Password:</td>
<td class = "TextField"><input type = "password" name = "pwd"/></td>
<tr>
<td></td>
<td class = "Information"><em>6-20 characters</em></td>
</tr>
<tr>
<td id = "ErrorPassword2"></td>
<td class = "FieldName">Confirm Password:</td>
<td class = "TextField"><input type = "password" name = "confirmPwd"/></td>
<tr>
<td></td>
<td class = "Information"><em>just in case you didn't make mistakes!</em></td>
</tr>
<tr>
<td id = "ErrorFname"></td>
<td class = "FieldName">First Name:</td>
<td class = "TextField"><input type = "text" name = "fName"/></td>
<tr>
<td></td>
<td class = "Information"><em>optional</em></td>
</tr>
<tr>
<td id = "ErrorLname"></td>
<td class = "FieldName">Lastname:</td>
<td class = "TextField"><input type = "text" name = "lName"/></td>
<tr>
<td></td>
<td class = "Information"><em>(optional)</em></td>
</tr>
<tr>
<td><input type = "submit" value = "Submit"/></td>
</tr>
</form>
</table>
<script>
$('#your-form').submit(validateForm);
</script>