Click here to Skip to main content
15,868,016 members
Articles / Web Development / IIS
Article

A reusable form validator for ASP pages

Rate me:
Please Sign up or sign in to vote.
4.29/5 (7 votes)
27 Jun 2001CPOL3 min read 160.3K   1.2K   40   23
Some functions for client side HTML forms validation. Never write them again!

What it's all about

When writing a web application using ASP, you often have to create HTML based input forms for all sorts of data. E.g. user registration pages, questionnaires, data capture templates and so on. Most of the time this data is the further processed and stored somewhere, usually in a database.

To make sure that only valid data is entered, some checks should occur before it is stored somewhere. Valid means here reasonable in the current context (e.g. no birthdays in the future) as well as suitable for the data store, like limitations in length or data type.

This validation can take place on the client side via JavaScript or on the server within the receiving Active Server Page. Both have well known advantages and disadvantages. This article contains a method for the JavaScript variant.

Writing the same code again and again

As I wrote various web applications during the last years I had quite often the feeling that I'm doing the same stuff again and again, and actually it was more or less the same. After reusing my old stuff with cut-and-paste a couple of times I decided to build a more general solution, that can be used in all upcoming projects.

The mission objectives

  • Write once use everywhere
  • No dynamic code generation (ASP scripts that generate client side JavaScript.....)
  • Separate JavaScript code from HTML (move it into a separate file)
  • Browser independent

The solution

As I ruled out dynamic code generation, I had to come up with a trick to make the validation code independent of the names of the input fields and to provide the functionality to be able to check only some of the fields. This is the important point of the solution, as the input fields can be accessed from JavaScript via their names or their indices. Both may vary.
<script language="JavaScript" src="/formvalidator.js"></script>

<form name="RegisterUserForm" action="register.save.asp" method="post" 
      OnSubmit="return CheckForm(document.RegisterUserForm);">

Logon Name: <INPUT TYPE="TEXT" NAME="logon_name" VALUE="">

<INPUT TYPE="HIDDEN" NAME="CHK_logon_name" VALUE="STR|5|255|True|Logon name">

</form>

So what are we doing here? The first two lines are more or less stratight forward. Include the JavaScript file that contains the form checker code and include an OnSubmit event handler. The interesting part is the last hidden input field. It contains instructions for the form checker. In this case they are:

  • The field to be checked has the name logon_name
  • It's a string value
  • Minimum of 5 characters
  • Maximum of 255
  • It's a mandatory field
  • The friendly name (for the error message) is Logon name

For strings it's not too far beyond what HTML can do for you right out of the box. Basically only the enforcement of mandatory fields and the minimum string length are enhancements. It gets more interesting when it comes to numeric values and dates.

According to the features for strings the form checker checks these values for numeric values (integers and floats):

  • Minimum and maximum values (not character length)
  • Validity of the string. Only numeric characters are allowed.

And for dates:

  • Validity of the string. Format of the string
  • Minimum and maximum date
  • Validity of the date (e.g February 30th is not allowed)

So what the CheckForm function actually does, is to go through all form elements of the form passed as parameter. It the looks for the CHK_ prefix and interprets the values as instructions to check the content of the corresponding field.

Some further thoughts

The beauty of the solution lies in the fact, that you don't further mess up your ASP code with JavaScript stuff. But there is more. What if you would generate all the input fields automatically, let's say from some meta data that describes the design of your data store? So your ASP page would just create all the necessary input fields and - as it has the data type information right at hand - the hidden fields for the validation check. Then you get the proper checking routines for free.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Germany Germany
If you are not living on the edge you are wasting space Wink | ;)

Comments and Discussions

 
Questionhi!im new to java and need some help ere.. Pin
blackboxer25-Feb-07 23:19
blackboxer25-Feb-07 23:19 
AnswerRe: hi!im new to java and need some help ere.. Pin
Christian Graus25-Feb-07 23:27
protectorChristian Graus25-Feb-07 23:27 
QuestionWhy not use Validator control at server side? Pin
Jeason Zhao1-Sep-04 20:29
Jeason Zhao1-Sep-04 20:29 
GeneralDate Format again Pin
Member 119834324-Jun-04 13:19
Member 119834324-Jun-04 13:19 
GeneralNew code Pin
28-Jan-03 22:20
suss28-Jan-03 22:20 
Generalsome modifications Pin
DioX19-Oct-02 21:44
DioX19-Oct-02 21:44 
GeneralRe: some modifications Pin
Horatiu CRISTEA3-Sep-03 4:13
Horatiu CRISTEA3-Sep-03 4:13 
GeneralDATE-format Pin
5-Jun-02 2:43
suss5-Jun-02 2:43 
GeneralRe: DATE-format Pin
Christian Tratz5-Jun-02 4:04
Christian Tratz5-Jun-02 4:04 
GeneralFocus on field with error Pin
5-Jun-02 1:54
suss5-Jun-02 1:54 
GeneralRe: Focus on field with error Pin
Anonymous18-Mar-03 18:49
Anonymous18-Mar-03 18:49 
GeneralRe: Focus on field with error Pin
PR-N-HOLE7-Oct-04 9:52
PR-N-HOLE7-Oct-04 9:52 
GeneralNew Feature added Pin
15-Apr-02 15:35
suss15-Apr-02 15:35 
Generaljust like that Pin
subas25-Feb-02 5:05
subas25-Feb-02 5:05 
GeneralDisabling of validation Pin
23-Jan-02 17:12
suss23-Jan-02 17:12 
GeneralRe: Disabling of validation Pin
Christian Tratz23-Jan-02 21:24
Christian Tratz23-Jan-02 21:24 
QuestionWhat about radio buttons Pin
14-Nov-01 11:58
suss14-Nov-01 11:58 
AnswerRe: What about radio buttons Pin
Mal Ross2-Apr-02 3:36
Mal Ross2-Apr-02 3:36 
GeneralToo Restricting!!! Pin
29-Jun-01 7:12
suss29-Jun-01 7:12 
GeneralRe: Too Restricting!!! Pin
2-Jul-01 23:38
suss2-Jul-01 23:38 
GeneralRe: Too Restricting!!! Pin
3-Jul-01 8:12
suss3-Jul-01 8:12 
GeneralRe: Too Restricting!!! Pin
Christian Tratz4-Jul-01 0:21
Christian Tratz4-Jul-01 0:21 
GeneralRe: Too Restricting!!! Pin
15-Feb-02 15:47
suss15-Feb-02 15:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.