Form Validator : Form Validation « Form Control « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Dojo toolkit
7. Event
8. Event onMethod
9. Ext JS
10. Form Control
11. GUI Components
12. HTML
13. Javascript Collections
14. Javascript Objects
15. Javascript Properties
16. jQuery
17. Language Basics
18. Mochkit
19. Mootools
20. Node Operation
21. Object Oriented
22. Page Components
23. Rico
24. Scriptaculous
25. Security
26. SmartClient
27. Style Layout
28. Table
29. Utilities
30. Window Browser
31. YUI Library
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
JavaScript DHTML » Form Control » Form Validation 
Form Validator
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Form Validator</title>
<style type="text/css">
.link{font-family:verdana,arial,helvetica; font-size:8pt; color:#003399; font-weight:normal}
.link:hover{font-family:verdana,arial,helvetica; font-size:8pt; color:#FF9900; font-weight:normal}
.header{font-family:arial,verdana,helvetica; font-size:30pt; color:#CC0000; font-weight:bold}
</style>
</head>
<body bgcolor="#FFFFFF">
<center><span class="header">Form Validator</span></center>
<center><br>

<!--------------------------------------BEGIN REQUIRED----------------------------------------->
<!--BEGIN THE FORM-->
<table style="border:3 solid #CC0000" bgcolor="#EFEFEF"><tr><td>
<font face="verdana,arial,helvetica" size="-1" color="#000000">

<form name="main" method="get" action="http://www.YourServer.com/cgi-bin/SomeCgiFile.cgi">
<input type="text" width="30" name="name" style="border:1 solid #000000"> &lt;&lt; Name<br>
<input type="text" width="30" name="address" style="border:1 solid #000000"> &lt;&lt; Address<br>
<input type="text" width="30" name="age" style="border:1 solid #000000"> &lt;&lt; Age<br>
<input type="text" width="30" name="zip" style="border:1 solid #000000"> &lt;&lt; Zip<br><br>

<center><input type="button" value="Submit" onClick="javascript:validate();" style="border:1 solid #000000; cursor:pointer; cursor:hand; width:120"> <input type="reset" style="border:1 solid #000000; cursor:hand; cursor:pointer; width:120"></center>
</form>
</font>
</td></tr></table>
<!--END THE FORM-->

<!--BEGIN FORM VALIDATION SCRIPT-->
<script language="JavaScript1.2">
/* Written by Premshree Pillai
   Created 2:22 PM 5/12/02
   http://www.qiksearch.com
   premshree@hotmail.com */
/* Visit http://www.qiksearch.com/javascripts.htm for FREE scripts */
/* Location of script : http://www.qiksearch.com/javascripts/form-validator.htm */

var alphaChars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
var numChars="0123456789";
var error;
var error_n;
var error_ad;
var error_a;
var error_z;
var errormsg;

//--------------------------Customise-------------------------------
var isNameReq=true// True if Name field required else False
var isAddressReq=true// True if Address field required else False
var isAgeReq=false// True if Name Age required else False
var isZipReq=true// True if Name Zip required else False
//------------------------------------------------------------------

function reset_error()
{
 error_n=false;
 error_ad=false;
 error_a=false;
 error_z=false;
 errormsg='Following Errors Occured ::\n_____________________________\n\n';
}

function validate_name()
{
 if(isNameReq)
 {
  if(document.main.name.value=="")
  {
   errormsg+='Please enter your Name.\n';
   error_n=true;
   document.main.name.focus();
  }
 }
 for(var i=0; i<document.main.name.value.length; i++)
 {
  for(var j=0; j<alphaChars.length; j++)
  {
   if(alphaChars.charAt(j)==document.main.name.value.charAt(i))
   {
    break;
   }
   else
   {
    if(j==(alphaChars.length-1))
    {
     errormsg+='"' + document.main.name.value.charAt(i'"' ' is an invalid character for Name.\n';
     error_n=true;
    }
   }
   if(error_n)
   {
    document.main.name.select();
   }
  }
 }
}

function validate_address()
{
 if(isAddressReq)
 {
  if(document.main.address.value=="")
  {
   errormsg+='Please enter your Address.\n';
   error_ad=true;
   if(!error_n)
   {
    document.main.address.focus();
   }
  }
 }
}

function validate_age()
{
 if(isAgeReq)
 {
  if(document.main.age.value=="")
  {
   errormsg+='Please enter your Age.\n';
   error_a=true;
   if((!error_n)&&(!error_ad))
   {
    document.main.age.focus();
   }
  }
 }
 for(var i=0; i<document.main.age.value.length; i++)
 {
  for(var j=0; j<numChars.length; j++)
  {
   if(numChars.charAt(j)==document.main.age.value.charAt(i))
   {
    break;
   }
   else
   {
    if(j==(numChars.length-1))
    {
     errormsg+='"' + document.main.age.value.charAt(i'"' ' is an invalid character for Age.\n';
     error_a=true;
    }
   }
   if(error_a)
   {
    if((!error_n)&&(!error_ad))
    {
     document.main.age.select();
    }
   }
  }
 }
}

function validate_zip()
{
 if(isZipReq)
 {
  if(document.main.zip.value=="")
  {
   errormsg+='Please enter Zip.\n';
   error_z=true;
   if((!error_n)&&(!error_ad)&&(!error_a))
   {
    document.main.zip.focus();
   }
  }
 }
 for(var i=0; i<document.main.zip.value.length; i++)
 {
  for(var j=0; j<numChars.length; j++)
  {
   if(numChars.charAt(j)==document.main.zip.value.charAt(i))
   {
    break;
   }
   else
   {
    if(j==(numChars.length-1))
    {
     errormsg+='"' + document.main.zip.value.charAt(i'"' ' is an invalid character for Zip.\n';
     error_z=true;
    }
   }
   if(error_z)
   {
    if((!error_n)&&(!error_ad)&&(!error_a))
    {
     document.main.zip.select();
    }
   }
  }
 }
}

function validate()
{
 reset_error();
 validate_name();
 validate_address();
 validate_age();
 validate_zip();

 if(error_n||error_ad||error_a||error_z)
 {
  error=true;
 }
 else
 {
  error=false;
 }
 if(!error)
 {
  document.main.submit();
 }
 else
 {
  alert(errormsg);
 }
}

</script>
<!--END FORM VALIDATION SCRIPT-->
<!--------------------------------------END REQUIRED------------------------------------------->

</center><br>

<table align="center" width="400"><tr><td>
<font face="verdana,arial,helvetica" size="-1" color="#000000">

This is a JavaScript Form validator. When you submit the form, it validates the data you entered in the various fields.
<br><br>You are allowed to enter only alphabetical characters in the "Name" field. You can enter only numerical data in the "Age" and "Zip" fields.
<br><br>You can also choose which fields are "required" by the user to be filled.
<hr style="color:#CC0000; height:1px; width:100%">
<a href="http://www.qiksearch.com" class="link">&#169 2002 Premshree Pillai. All rights reserved.</a>
</font>
</td></tr></table>

</body>
</html>

           
         
    
  
Related examples in the same category
1. Form Validator: time, date email, phone number, age etc
2. Form Validate: is blank, is digit and is integer
3. Log in form validation
4. Log in email validation
5. Comprehensive form validation
6. Illegal sub string validation
7. Email form validation
8. Form valiation for empty data, file name
9. Email Address Validate
10. Time Validate
11. URL Validate
12. Use JavaScript to check the password input
13. Form value validation and onsubmit action
14. Check form input value and alert user by change the control background
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.