You can use forms on your website to get information from the visitor. However a user might make an error, or forget and entry in a form. Ignorant and malicious users may deliberately enter trash into your form in an effort to break your application.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Easy Java Script Form Validation

You can use forms on your website to get information from a visitor. However a user might make an error, or forget an entry in a form. Ignorant and malicious users may deliberately enter trash into your form in an effort to break your application. For this reason, you need to perform validation on the forms contents before you process it.

Java Script is ideal for form validation because it can verify the forms contents before it is submitted to the server. Thus a malicious user can mess around with your form all day without causing a denial of service for other users. In this article I will show you how to use Java Script to validate your forms.

The example below is a simple search box form consisting of a text box named "term" and a button named "Search". The onClick event of the Search button passes the form as an argument to a function named "Validate". The Validate function will check the form and, if its okay, submit it to the server.

<form name="search" method="post">
<input name="term" size=15 maxlength=50>
<input type="button" value="Search" onClick="Validate(this.form);">
</form>

The form tag allows you to submit the form using either the POST method or the GET method. The GET method causes the browser to append all data from the submitted form to the URL. We don't want this because the user can then edit the form information directly in the browsers address bar, bypassing the Validate function entirely.

The POST method causes the browser to send all the data from the submitted form in the HTTP header where it is not visible to the user. In this article, I'm not going to go into detail about what to do at the server end for processing the form. But, if for example you were using Active Server Pages (ASP), you would use the Request.QueryString("term") function to retrieve the form data with the GET method.

You would use the Request.Form("term") function to retrieve the form data with the POST method. So the user can play with the address bar URL all they want and your server side application will ignore it.

Below is code for the most basic Validate function. You would place this code in the head section of the Web page containing the form. It only checks for the case of an empty term text box. If the text box is empty it displays a message in a dialog box to the user. If the term text box is not empty, it submits the form to the server.

<script language="JavaScript">

function Validate(form)
{
   if(form.term.value == "")
   {
      alert("Enter a search term");
   }
   else
   {
      form.submit();
   }
}

</script>

This Validate function could be expanded to check the text box for other errors, such as the user entering only spaces. However, as you add code for more checks, the function would become long and complicated. Therefore it is best to design a Validate function that calls other functions for specific procedures. Below is a function that checks if the term text box is empty. Note how it is called by the Validate function.

<script language="JavaScript">

function Empty(field)
{
   if(field == "")
   {
      return(true);
   }
   else
   {
      return(false);
   }    
}

function Validate(form)
{
   var error = 0;

   if(Empty(form.term.value)) error = 1;

   if(error)
   {
      alert("Error");
   }
   else
   {
      form.submit();
   }
}

</script>

Web Design Sections

RSS Feed RSS Feed

Easy Java Script Code
Easy Slide Show Code
Easy Slide Show Code with Mouseover Pause
Easy Slide Show Code With Linked Slides
Slide Show with Different Size Images
Easy Java Script Timer Code
Easy Java Script Windows
Easy Rollovers
Easy Picture Transition Effects
Easy Moving Popup Code
Easy Picture Zoom Code
Easy Picture Panning Code
Easy Java Script Animation
Easy JavaScript Picture Selector Code
Easy Animated Banners with Java Script
Easy Java Script Form Validation
Easy Drag n Drop Code
Easy Graph Generating Code
Easy Code to Sort a Table by Column
Easy Expanding Menu Code
Web Designer's Reference
Object-Oriented JavaScript
Easy Fading Text Banner Code
Easy Expanding Banner Code
Calendars for Your Website
Date Picker For Your Web Site
Calculators For Your Web Site
Loan Payment Calculator
Length Units Converter
Body Mass Index
Fahrenheit to Celsius Converter
Decimal to Hexidecimal Converter
Easy Code for Screen Tape Adding Machine
Code for Java Script Circle⁄Sphere Calculator
Code for Java Script Cube⁄Box Volume Calculator
Round a Float to 4 Digits to the Right of the Decimal Point
Disable IE8 Accelerators on Your Website
Prevent Automated Form Submissions With Java Script
Make Your Own Graphical Digital Clock
Let Your Web site Visitors Set Font Size
How to Disable the Browser Back Button
Add More Bang to Your Content With Keyword Popup Menus
Put a Music Off Switch on Your Webpage
Java Script Random Password Generator
Password Protection Using the JavaScript Cookie Method
Password Protection Using the Frames Method
Replace Drop-down with Drag-and-drop
Submit Forms Without CGI
Code for a Less annoying Popup Window
Java Script Comparison Operators
Using the Java Script Array Object
Using the Java Script Date Object
Java Script Message Boxes
Java Script Trim Function
Convert Mixed Number to Decimal
How to Shuffle the Deck With Java Script
Web Site Menus : Which Section Am I In?
How Far Did the User Scroll?
Where Did the User Click?
Four Ways to Use Java Script Event Handlers
Create Your Own Database Using Only Notepad : CDV


[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2009 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269