Validating User Input : Validation : Form Control : JavaScript DHTML examples (example source code) Organized by topic

JavaScript DHTML
C++
PHP


JavaScript DHTML  »  Form Control   » [  Validation  ]   
 



Validating User Input

Please note that some example is only working under IE or Firefox.


/*
JavaScript Unleashed, Third Edition
by Richard Wagner and R. Allen Wyke 

ISBN: 067231763X
Publisher Sams CopyRight 2000

*/
<html>
<head>
<title>Contact Information</title>
<script language="JavaScript">
<!-- script start
// Ensure that mandatory fields of
// form have been completed
function validateComplete(formObj)
{
     if (emptyField(formObj.firstName))
          alert("Please enter your first name.");
     else if (emptyField(formObj.lastName))
          alert("Please enter your last name.");
     else if (emptyField(formObj.address1)
          && emptyField(formObj.address2))
          alert("Please enter your address.");
     else if (emptyField(formObj.city))
          alert("Please enter your city or town.");
     else if (emptyField(formObj.state))
          alert("Please enter your state.");
     else if (emptyField(formObj.email))
          alert("Please enter your E-mail address.");
     else return true;
   
     return false;
}
   
// Check to see if field is empty
function emptyField(textObj)
{
     if (textObj.value.length == 0return true;
     for (var i=0; i<textObj.value.length; ++i) {
          var ch = textObj.value.charAt(i);
          if (ch != ' ' && ch != '\t'return false;
     }
     return true;
}
   
// script end -->
</script>
</head>
<body>

<h1>Contact Information (U.S.)</h1>
<form name="myform" action="actionURL" method="post"
 onSubmit="return validateComplete(document.myform)">
<pre>
First Name:<input type="text" name="firstName">
Last Name: <input type="text" name="lastName">
Address:   <input type="text" name="address1">
           <input type="text" name="address2">
</pre>
   
City/Town:
<input type="text" name="city" size=12>
State:
<input type="text" name="state" size=2>
Zip Code:
<input type="text" name="zip" size=5>
   
<pre>
Home Phone:<input type="text" name="homePhone" size=12>
Work Phone:<input type="text" name="workPhone" size=12>
FAX:       <input type="text" name="FAX" size=12>
E-mail Address: <input type="text" name="email">
Quest:          <input type="text" name="quest">
Favorite Color: <input type="text" name="favColor">
</pre>
<hr>
<input type="submit" name="submit" value="Submit">
</form>
<body>
<html>

           
       
Related examples in the same category
1.  Form validation: Not Empty, Valid Radio, is Number, string length, EMail Address
2.  Validate an input field with minimum and maximum values
3.  Validate an field with a maximum number of characters
4.  Phone Number Validation
5.  Creating Reusable Validation Code
6.  Credit Card Validation
7.  Money Format
8.  Validate email address
9.  Validate a number
10.  TextField input length validator
11.  Password field validator
12.  TextField validator: email, IP address and URL
13.  Must be at least 3 characters and not more than 8Has Download File
14.  Only characters are allowedHas Download File
15.  Can be emptyHas Download File
16.  Must be a valid email addressHas Download File
17.  ComboBox(list box): Must be selected one Has Download File
18.  InputBox: value must be between 10 and 90Has Download File
19.  Not stop when the first failed validation is encounteredHas Download File
20.  A CSS is used to highlight the fields which failed to validateHas Download File
21.  Need to select a fileHas Download File
22.  A simple form with two passwords that must have the same valueHas Download File
23.  Shows the usage of callback functions for checking a fieldHas Download File
24.  Javascript validation frameworkHas Download File
25.  Form validator libraryHas Download File
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.