Sullivan Market

a web based full-circle development system



My JavaScript Tutorials - Form Validation

Here's the Javascript to Validate the Contents of a Form:

function setValues() {

}

function check()
{
var missing = 0;
var msg = "Please correct the following error(s):\n\n";
var p1 = document.forms[0].pAccessCode1.value;
var p2 = document.forms[0].pAccessCode2.value;

if (document.forms[0].pCliFirst.value == "" || space_count(document.forms[0].pCliFirst)){
missing ++;
msg = msg + " - Client's First Name" + "\n";
}

if (document.forms[0].pCliLast.value == "" || space_count(document.forms[0].pCliLast)){
missing ++;
msg = msg + " - Client's Last Name" + "\n";
}


if (document.forms[0].pEmail.value == "" || space_count(document.forms[0].pEmail)){
missing ++;
msg = msg + " - E-mail Address" + "\n";
}

if (document.forms[0].pZip.value == "" || space_count(document.forms[0].pZip) || checkLength(document.forms[0].pZip,5) || isNaN(document.forms[0].pZip.value) ){
missing ++;
msg = msg + " - ZIP/Postal Code" + "\n";
}

if (document.forms[0].pUsername.value == "" || space_count(document.forms[0].pUsername)){
missing ++;
msg = msg + " - Client's Username" + "\n";
}

if (document.forms[0].pAccessCode1.value == "" || space_count(document.forms[0].pAccessCode1) || checkLength(document.forms[0].pAccessCode1,5)){
missing ++;
msg = msg + " - Your Password" + "\n";
}

if (document.forms[0].pAccessCode2.value == "" || space_count(document.forms[0].pAccessCode2) || checkLength(document.forms[0].pAccessCode2,5)){
missing ++;
msg = msg + " - Your Re-entered Password" + "\n";
}

if ( p1 != p2 ) {
missing++;
msg = msg + " - Your Password and Your Re-entered Password Do Not Match \n";
}
//Variables for validating Email addresses for proper format
var str = document.forms[0].pEmail.value;
var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
var skip = false;
if (str.length > 0){
if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
skip = true;
}
if (skip == false){
missing++;
msg = msg + " - Your Email, " + str + ", is invalid." + "\n";
}
}
if (missing > 0){
alert(msg);
return false;
}
else
return true;
}

Here's the Form Being Validated:

<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT language=JavaScript
src="common/javascript/suformupdate.js"></SCRIPT>
</HEAD>
<BODY>

<FORM name=frm onsubmit="return check()" action=adduser.asp
method=post>
<TABLE height=146 cellSpacing=0 cellPadding=0 width="85%"
align=center border=0>
<TBODY>
<TR>
<TD align=middle height=21>
<P align=left><FONT size=6><SPAN
class=PageTitleClass><SPAN
class=PageTitleClass><FONT size=7>Signup</FONT>
<P></P>
<P></P>
<P></P>
<P></P></SPAN></SPAN></FONT><!--border
content-->
<TR>
<TD align=middle height=1>
<TABLE height=233>
<TBODY>
<TR>
<TD colSpan=2 height=21><B>All fields are
required</B></TD></TR><!--- Required Fields ---
>
<TR>
<TD vAlign=top height=25>First Name</TD>
<TD vAlign=top height=25><INPUT maxLength=25
name=pCliFirst></TD></TR>
<TR>
<TD vAlign=top height=25>Last Name</TD>
<TD vAlign=top height=25><INPUT maxLength=25
name=pCliLast></TD></TR>
<TR>
<TD vAlign=top height=25>E-mail:</TD>
<TD vAlign=top height=25><INPUT size=35
name=pEmail></TD></TR>
<TR>
<TD vAlign=top height=25>Zip Code:</TD>
<TD vAlign=top height=25><INPUT maxLength=5 size=10
name=pZip></TD></TR>
<TR>
<TD vAlign=top height=25>

</TD>
<TD vAlign=top height=25><INPUT
name=pUsername></TD></TR>
<TR>
<TD vAlign=top height=25>Password:</TD>
<TD vAlign=top height=25><INPUT type=password maxLength=12
name=pAccessCode1></TD></TR>
<TR>
<TD vAlign=top height=30>Password Verify:</TD>
<TD vAlign=top height=30><INPUT type=password maxLength=12
name=pAccessCode2></TD></TR></TBODY></TABLE>
<P><input type="Submit" name="cmdSearch" value="Search">
<input type="Reset" name="cmdReset"
value="Reset"></P></TD>
<TR>
<TD align=middle height=40>
<P></P>
<P></P>
<P></P>
<P></P></TD></TR></TBODY></TABLE></FORM>

</BODY>
</HTML>

Note:: Make sure that, for this example, you place all the files within the same folder.

Back To JavaScript Tutorials

©2012 SullivanMarket.com