Amazon.com: PC Books || Educational Software || Magazines
Amazon: Books-CA || Software-CA || Books-UK || Software-UK

HTML | Javascript | ASP | PHP | VBScript | SQL | Hardware | PC FAQ| WinXP|

Learn to Build, Upgrade, or Repair your Computer Ebook + PC Safety 101 kwwebservice.com Reasonably priced web development & hosting
JavaScript
Introduction
Variables & Arrays
Functions
If Statements
Case Statements
Loop Statements
Operators
Form Validation
Alert
Date Objects
Window Objects
Javascript Books
Books @ CA Amz
Books @ UK Amz
Books @ US Amz

  :: Home
PC Topics
  :: Build A PC
  :: Windows XP
  :: PC Help
Tutorials
  :: HTML
  :: JavaScript
  :: ASP
  :: PHP
  :: VBScript
  :: SQL
Miscellaneous
  :: Code/Scripts
  :: Forum
  :: Links
  :: Contact us
  :: Tell A Friend
  :: In Somali
  ::
Web Articles
  Top Web Hosting
Reviews
  Processing
Online Card Payment Guide
  Domain name
registration & buying guide
  Getting
a website online guide
  Search Engine
submission & optimization tips
Netfirms Web Hosting
Free trial
Quality business correspondence.
Yahoo! Search Marketing

Validate html forms with javascript

Javascript is used to validate forms, display information in text box or when button is clicked, and other nice activities. Form validation means checking that proper information are entered in the form fields before submission. Forms are not of much use in terms of processing and posting when you are limited to HTML and you don't have access to other script such as CGI. This discussion is limited to form validations. If you want learn how to create html forms, check out the html form page.

The following example alerts the value entered the text fields:
<html>
<head>
</head>
<body>
<FORM name="fm">
Type your first name:
<INPUT type="text" name="first"><br>
Type your last name:
<INPUT type="text" name="last">
<INPUT type="button" name="dis" value="Display" onClick='alert("You say your name is: "+document.fm.first.value+"
"+document.fm.last.value)'>
</FORM>
</body>
</html>
The only JavaScript concept of this example is OnClick. onClick event handler responds when object is clicked and executes the JavaScript code or function. In this case, it executes alert box . The alert box displays text box values.
Here is the result of this example:
Type your first name:
Type your last name:
The following example explains displaying list box selected option:
<html>
<body>
<html>
<body>
<FORM name="fm2">
Select one: <select name="selec" onchange='alert("You selected "+document.fm2.selec.value)'><br>
<option>Select One
  <option value="Java">Java
  <option value="C++">C++
  <option value="Cobol">Cobol
</select>
</FORM>
</body>
</html>
</body>
</html>
We use onChange event handler, to display alert box with the selected value. OnChange executes the specified JavaScript code or function on the occurance of a change event. In this case, it executes an alert box that displays selected value of the select box.
Here is the result of this example:
Select one:

Form Validations

The most practical business use of javascript is object validations.  Making sure that the user's information is as it required.  This sample is java script code that checks and alerts if the fields of the form are empty. 
<HTML>
<HEAD>
<SCRIPT LANGUAGE=JAVASCRIPT>
function validate(formCheck) //Function with a parameter representing a form name.
{
    if (formCheck.name.value =="")
    {
        alert("Please provide your name:"); 
        formCheck.name.focus();
        return false;
    }
   var mail=formCheck.email.value
   if (mail.indexOf("@.") == -1)
    {
        alert("Please type a valid email:");
        formCheck.email.focus();
        return false;
    }
    return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="inform" action="" method "post">
   Your Name: <INPUT type=text NAME="name" value=""SIZE=20><br>
   Your Eamil: <INPUT type=text NAME="email" value="" SIZE=30><br>
   <INPUT type=button name="submit" value=" Send " onClick = "return validate(inform)"; >
   <INPUT type=reset name=reset value=" Clear ">
</FORM>
</BODY>
</HTML>
How it works
We created a function called validate which takes a parameter. This parameter represents the form name.
Using if statement, we compare the text field name with empty string. If this field is equal to the empty string, then displayed alert box and set the focus to this field. We then return false to avoid the action to be continued. Email field is slightly different. For this field, we check if @ and . are provided. If not, we alert the user, set the focus and return false.
If the two controls pass true value, then the function returns true. In this case, onClick event handler returns true and nothing happens.
Note: If the form action proceeds to another page, use onSubmit instead of onClick. onSubmit executes the function and continues processing the file defined in the form action.

Operators Alert, Prompt, Confirm boxes
Bluehost.com Web Hosting $6.95
PC System Tools
SpyCleaner
Registry Mechanic
4Diskclean Pro
Health PC Club
TweakNow
Registry Washer
SpyAnyWhere
System Lifeguard
...More/Details

PC Articles
  Computer Safety
prevent viruses & Spyware