admin管理员组文章数量:1344926
I want my form to only submit if all my JavaScript functions return true.
Here is my Javascript code:
function validateUserName(NewUser)
{
var u = document.forms["NewUser"]["user"].value
var uLength = u.length;
var illegalChars = /\W/; // allow letters, numbers, and underscores
if (u == null || u == "")
{
alert("You left Username field empty");
return false;
}
else if (uLength <4 || uLength > 11)
{
alert("The Username must be between 4 and 11 characters");
return false;
}
else if (illegalChars.test(u))
{
alert("The username contains illegal characters");
return false;
}
else
{
return true;
}
}
function validatePassword(NewUser)
{
var p = document.forms["NewUser"]["pwd"].value
var cP = document.forms["NewUser"]["confirmPwd"].value
var pLength = p.length;
if (p == null || p == "")
{
alert("You left the password field empty");
return false;
}
else if (pLength < 6 || pLength > 20)
{
alert("Your password must be between 6 and 20 characters in length");
return false;
}
else if (p != cP)
{
alert("The passwords do not match!");
return false;
}
else
{
return true;
}
}
function validateEmail(NewUser)
{
var e = document.forms["NewUser"]["email"].value
var eLength = e.length;
var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
if (eLength == "" || eLength == null)
{
alert("You left the email field blank!");
return false;
}
else if (e.match(illegalChars))
{
alert("ILEGAL CHARECTORS DETECTED EXTERMINATE");
return false;
}
else
{
return true;
}
}
function validateFirstName(NewUser)
{
var f = document.forms["NewUser"]["fName"].value;
var fLength = f.length;
var illegalChars = /\W/;
if(fLength > 20)
{
alert("First Name has a max of 20 characters");
return false;
}
else if (illegalChars.test(f))
{
alert("Numbers,letter and underscores in first name only");
return false;
}
else
{
return true;
}
}
function validateLastName(NewUser)
{
var l = document.forms["NewUser"]["lName"].value;
var lLength = l.length;
var illegalChars = /\W/;
if(lLength > 100)
{
alert("Last Name has a max of 100 characters");
return false;
}
else if (illegalChars.test(f))
{
alert("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();
}
Here is my HTML:
<table id = "SignUpTable">
<p id = "SubHeading">Sign Up.</p>
<form name = "NewUser" onsubmit= "validateForm()" action = "">
<tr>
<td class = "FieldName">Username:</td>
<td class = "TextField"><input type = "text" name = "user"/></td>
</tr>
<tr>
<td class = "Information"><em>Must be 4-11 characters.<br/>Only numbers, letters and underscores.</em></td>
</tr>
<tr>
<td class = "FieldName">Email:</td>
<td class = "TextField"><Input type = "text" name = "email"/></td>
<tr>
<td class = "Information"><em>We need this to verify your account.</em></td>
</tr>
<tr>
<td class = "FieldName">Password:</td>
<td class = "TextField"><input type = "password" name = "pwd"/></td>
<tr>
<td class = "Information"><em>6-20 characters</em></td>
</tr>
<tr>
<td class = "FieldName">Confirm Password:</td>
<td class = "TextField"><input type = "password" name = "confirmPwd"/></td>
<tr>
<td class = "Information"><em>just in case you didn't make mistakes!</em></td>
</tr>
<!-- Optional -->
<tr>
<td class = "FieldName">First Name:</td>
<td class = "TextField"><input type = "text" name = "fName"/></td>
<tr>
<td class = "Information"><em>optional</em></td>
</tr>
<tr>
<td class = "FieldName">Lastname:</td>
<td class = "TextField"><input type = "text" name = "lName"/></td>
<tr>
<td class = "Information"><em>(optional)</em></td>
</tr>
<tr>
<td><input type = "submit" value = "Submit"/></td>
</tr>
</table>
</form>
</div>
And here is a JS Fiddle:
/
(Please ignore the bad sizing in the form)
I want my form to only submit if all my JavaScript functions return true.
Here is my Javascript code:
function validateUserName(NewUser)
{
var u = document.forms["NewUser"]["user"].value
var uLength = u.length;
var illegalChars = /\W/; // allow letters, numbers, and underscores
if (u == null || u == "")
{
alert("You left Username field empty");
return false;
}
else if (uLength <4 || uLength > 11)
{
alert("The Username must be between 4 and 11 characters");
return false;
}
else if (illegalChars.test(u))
{
alert("The username contains illegal characters");
return false;
}
else
{
return true;
}
}
function validatePassword(NewUser)
{
var p = document.forms["NewUser"]["pwd"].value
var cP = document.forms["NewUser"]["confirmPwd"].value
var pLength = p.length;
if (p == null || p == "")
{
alert("You left the password field empty");
return false;
}
else if (pLength < 6 || pLength > 20)
{
alert("Your password must be between 6 and 20 characters in length");
return false;
}
else if (p != cP)
{
alert("The passwords do not match!");
return false;
}
else
{
return true;
}
}
function validateEmail(NewUser)
{
var e = document.forms["NewUser"]["email"].value
var eLength = e.length;
var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
if (eLength == "" || eLength == null)
{
alert("You left the email field blank!");
return false;
}
else if (e.match(illegalChars))
{
alert("ILEGAL CHARECTORS DETECTED EXTERMINATE");
return false;
}
else
{
return true;
}
}
function validateFirstName(NewUser)
{
var f = document.forms["NewUser"]["fName"].value;
var fLength = f.length;
var illegalChars = /\W/;
if(fLength > 20)
{
alert("First Name has a max of 20 characters");
return false;
}
else if (illegalChars.test(f))
{
alert("Numbers,letter and underscores in first name only");
return false;
}
else
{
return true;
}
}
function validateLastName(NewUser)
{
var l = document.forms["NewUser"]["lName"].value;
var lLength = l.length;
var illegalChars = /\W/;
if(lLength > 100)
{
alert("Last Name has a max of 100 characters");
return false;
}
else if (illegalChars.test(f))
{
alert("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();
}
Here is my HTML:
<table id = "SignUpTable">
<p id = "SubHeading">Sign Up.</p>
<form name = "NewUser" onsubmit= "validateForm()" action = "">
<tr>
<td class = "FieldName">Username:</td>
<td class = "TextField"><input type = "text" name = "user"/></td>
</tr>
<tr>
<td class = "Information"><em>Must be 4-11 characters.<br/>Only numbers, letters and underscores.</em></td>
</tr>
<tr>
<td class = "FieldName">Email:</td>
<td class = "TextField"><Input type = "text" name = "email"/></td>
<tr>
<td class = "Information"><em>We need this to verify your account.</em></td>
</tr>
<tr>
<td class = "FieldName">Password:</td>
<td class = "TextField"><input type = "password" name = "pwd"/></td>
<tr>
<td class = "Information"><em>6-20 characters</em></td>
</tr>
<tr>
<td class = "FieldName">Confirm Password:</td>
<td class = "TextField"><input type = "password" name = "confirmPwd"/></td>
<tr>
<td class = "Information"><em>just in case you didn't make mistakes!</em></td>
</tr>
<!-- Optional -->
<tr>
<td class = "FieldName">First Name:</td>
<td class = "TextField"><input type = "text" name = "fName"/></td>
<tr>
<td class = "Information"><em>optional</em></td>
</tr>
<tr>
<td class = "FieldName">Lastname:</td>
<td class = "TextField"><input type = "text" name = "lName"/></td>
<tr>
<td class = "Information"><em>(optional)</em></td>
</tr>
<tr>
<td><input type = "submit" value = "Submit"/></td>
</tr>
</table>
</form>
</div>
And here is a JS Fiddle:
http://jsfiddle/vK9aJ/
(Please ignore the bad sizing in the form)
Share Improve this question edited Jan 14, 2017 at 4:30 alexbt 17.1k6 gold badges81 silver badges89 bronze badges asked May 2, 2013 at 10:12 entropyentropy 3212 gold badges5 silver badges16 bronze badges 3- Plz don't paste whole code.paste only the code where you have been struck out. – MahaSwetha Commented May 2, 2013 at 10:14
- the jsfiddle you post have no code in it – a--m Commented May 2, 2013 at 10:15
- Sorry, won't do it again. – entropy Commented May 2, 2013 at 10:15
4 Answers
Reset to default 5function validateForm() {
if(validateUserName() && validatePassword() && validateEmail() && validateFirstName() && validateLastName()) return true;
else return false;
}
Depending on the return of your function you can chose to submit the form or not.
You can also optimize the previous answers this way:
If one of your functions returns false, then the expression will be set to false.
function validateForm() {
return validateUserName()
&& validatePassword()
&& validateEmail()
&& validateFirstName()
&& validateLastName()
;
}
Use this:
function validateForm() {
if (
validateUserName()
&& validatePassword()
&& validateEmail()
&& validateFirstName()
&& validateLastName()
) {
return true;
}
return false;
}
Call a single function, for example validate() and from it call the other functions which you have used. If all the functions return true, only then the function validate() returns true.
function validate()
{
if(validateUserName() && validatePassword() && validateEmail() && validateFirstName() && validateLastName())
return true;
else
return false;
}
本文标签: javascriptHow to make form only submit if all functions return trueStack Overflow
版权声明:本文标题:javascript - How to make form only submit if all functions return true? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743789762a2539347.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论