ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || RAM 
Search Site:

Home » Javascript » Article

Javascript Form Validation Function

Article by:  Jeff Anderson  ( 1362 ) (2/26/2002)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Summary: A javascript validation function that you can use to validate all types of forms.
Viewed: 554766 times Rating (253 votes): 
 0 out of 5
 Rate this Article  Read Comments  Post Comments

In several other tutorials, we have looked at various functions to check aspects of form validation :

Here we are going to bring these all together, by writing a ValidateForm function that can call these, or other similar functions. You can then adapt this script to validate the particular fields you need in the way you require.

Ammend your <form> tag, so it looks something like this:

<form name=form1 action=address.asp method=post onsubmit="javascript:return ValidateForm(this)">

What the onsubmit method does here is waits for the results of the ValidateForm function. If it returns true, our validation has succeeded - the form entries are in an acceptable form. If it returns false, there has been an error, and the user will need to correct it before being allowed to submit the form. So now let's have a look at the ValidateForm function itself. Exactly how it will look of course will depend on the fields you need to validate.

function ValidateForm(form)
   if(IsEmpty(form.account_number))    {       alert('You have not entered an account number')       form.account_number.focus();       return false;    }    if (!IsNumeric(form.account_number.value))    {       alert('Please enter only numbers or decimal points in the account field')       form.account_number.focus();       return false;       } return true; }

To understand this function, remember that the return statement exits from the function and returns the code to the calling statement. Therefore, if the field 'IsEmpty', we alert the user, set the focus of the form to the appropriate field, and return false as the result. The calling statement reads the false value and does not submit the form. The user remains on the same page, with the focus pointing them to the error of their ways.

If however all the checks are verified, at the end of the ValidateForm function, we have a return true statement, meaning the form will submit successfully.

Useful Links

CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'Javascript Form Validation function'
Javascript - Enable and Disable form elements
by Jeff Anderson
This is a relatively little known and under-used feature of javascript which can be very useful in guiding a user through a form. Using the disabled tag, you can switch on and off elements in a form.
Check IsNumeric Function
by Jeff Anderson
A javascript validation function to check whether the details entered by a user are numeric.
Javascript Onload Event
by Jeff Anderson
Sometimes you need to perform an action immediatley after the page has loaded. That's when the onLoad Event Handler comes in handy
Form Validation Function
by Jeff Anderson
A javascript validation function that you can use to validate all types of forms.
Javascript Get Selected Text
by Jeff Anderson
A cross-browser script to get text selected by the user
JavaScript Field Is Empty Form Validation
by Jeff Anderson
This javascript function allows you to check whether a form field has been completed or not.
Check Email Validation Function
by Jeff Anderson
A javascript validation function to check whether the user has entered a valid email address in a form.
Javascript onUnload Event Handler
by Jeff Anderson
The onUnload Event Handler allows you to perform an action as the user leaves the page.
Multiple submit buttons on a single form
by Kiran Pai
This script shows you how to submit the contents of a form to different programs depending on which Submit button you press. Additionally it also shows how to call two different functions when you press the Submit button.
Simple date validation
by Chris Hogben
function that takes a date in three parts, day, month and year - returns true if it's a valid date.

Recent Forum Threads
• Run a program both on windows and linux
• VERO.SurfCAM.v2014
• Schlumberger.Petrel.V2013.2
• Petrel.V2013.2
• Altair.HyperWorks.v12
• VoluMill.v6.1
• VoluMill.NEXION.6
• VERO.SurfCAM.v2014
• Schlumberger.Petrel.V2013.2

Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text

© Copyright 2001-2015