All source code in ASP/ VbScript Ask a ASP/ VbScript Pro Discussion Forum Categories All jobs in ASP/ VbScript
Quick Search for:  in language:    
Form,field,validation,most,important,function
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
ASP/ VbScript Stats

 Code: 281,486. lines
 Jobs: 101. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login

NEW! LEARNING CENTER
Special educational offers, white papers, webcasts, podcasts

  NEW! Download Rational Performance Tester V8 (download now)
  NEW! Download Rational Functional Tester V8 (download now)
  NEW! Download Rational Service Tester for SOA Quality V8 (download now)
  NEW! Teleconference: Quality In Action - Using Rational Quality Manager with Functional, Performance and Web Service Testing Products (download now)
  NEW! Introducing IBM Rational AppScan Developer Edition – easing security testing by non-security professionals (download now)

 

 


Latest postings for ASP/ VbScript.
Click here to see a screenshot of this code!Mess Officer Management System (Sistem Pengurusan Mess Pegawai)
By Mohd Fauzi on 11/23

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!



 
 
   

Client-Side Form Field Validation with VBScript and ASP

Print
Email
 
article
Submitted on: 5/18/2000 10:44:14 AM
By: Glenn Cook  
Level: Beginner
User Rating: By 7 Users
Compatibility:ASP (Active Server Pages), VbScript (browser/client side)

Users have accessed this article 62132 times.
 
 
     Form field validation is one of the most important functions in making successful web-based applications, but server-side validation is not always the answer!

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

Client-Side Form Field Validation with VBScript and ASP

Form field validation is one of the most important functions in making successful web-based applications. It protects your database from errors, filters bad data, and forces the user to think twice before submitting data with typos. For some reason though, documentation in many VBScript and JavaScript books on the market tend to gloss over this issue. I think it might be that field validation scripting is somewhere between learning the basics but not complicated enough for the advanced literature. The ASP books cover server-side validation (Remember? It's a server-side scripting language.) but this is not always an ideal approach to validating data. The problems with server-side validation are that it bogs down your server's resources when the user could handle the routines locally, and it offers a slower response time for the user when it actually finds errors. This tutorial is designed to take the load off the servers for a while and get the beginning weblication programmer closer to the intermediate level and beyond. Have fun!

The goal of the script:

When I click the submit button I want my script to check the field data in my form and either:

  1. Submit the data if it all looks good
  2. Show an alert box and do not send the data.

You  need a few tools:

  • The OnSubmit event.
  • A form name in your HTML code (e.g. <form METHOD="POST" ACTION="vbsample.asp" name="MyForm">)
  • A Submit Button: (e.g. <input TYPE="submit" VALUE="Submit Info!" name="submit">
  • An Input Box with name: (e.g. <input type="text" name="MyBox" size="10">)
  • Some "If..Then" statements
  • An "Alert Box"
  • The secret. (I love suspense.)

Let's see it work before we get into the guts of it!

  1. Type your name and submit!
  2. Enter nothing and submit!
  3. Enter one character and submit!



 

<SCRIPT LANGUAGE="VBScript">

<-- Option Explicit

dim validation

dim header

header = aspalliance.com

The first thing you want to do is tell the browser that the code you are about to run is VBScript.   The next thing you want to do is make some variables that we are going to use in our routine.
The "validation" variable is going to be used to help us determine whether we should send the form or not.  It will can be equal to either "True" or "False" (By the way, that's pretty much the secret ingredient!
)
The "Header" variable I will use to hold the string information for a MsgBox property.
Function MyForm_OnSubmit

validation = True

The next thing you want to do is make a function that is going to be called when a user clicks the "Submit Info" button. The function needs to be bound to your form name and the OnSubmit event so VB knows what it is that you are trying to submit.  I have named my form...."MyForm"
I have also made my "validation" variable equal to "True."   I will follow this with some "If...Then" statements that will try to change the variable to "False."
If Len(Document.MyForm.MyBox.Value) > 2 Then

MsgBox "You have entered too many characters!  You need fewer characters before I will submit this form",8, Header

validation = False

End If

My first "If...Then" basically says that If the Length of the value of the contents of Mybox in the form Myform within the current document is less than five characters Then show the Message Box(MsgBox). The properties of the message box include the message string, then the type of message box(8), and I add the header message.  The next thing I do is make our validation variable equal to False. You'll see why in a second.
The last thing I do is End this If statement.
If (Document.MyForm.MyBox.Value) = "" Then

MsgBox "You have forgotten to fill in the input box!  Why would you want to submit nothing in a one field form? C'mon, give me   something to work with!",8, Header

validation = False

End If

In this "If..Then" I make sure the user has at least entered something into the text input box.  If they haven't they will get a message box and once again the validation variable will be made equal to false.
If validation = True Then

MyForm_OnSubmit = True

Else

MyForm_OnSubmit = False

End If

End Function

</SCRIPT>

The last part of this routine is the secret.
All I do is test to see if any "If..Then" statement has made my validation variable equal to something other than "True".  If the validation variable equals "True" then MyForm_OnSubmit also equals "True."  If the variable equals something else other than "True" Then MyForm_OnSubmit will equal "False."   If the Function equals "False" then the OnSubmit event will not execute and the user is prompted to fix their mistake. Otherwise, the OnSubmit event fires and the form data is posted to the page that will process the submitted form. Simple!

Finally, End your If, tell VBScript to end the Function, and close the script with the SCRIPT tag.

Now granted, this is is a very simple example and in a real-world application you might have ten to twenty form fields to validate.   Although there is potential for your script becoming quite complicated you can use some subroutines that do some work for you.  Example:

Function MyForm_OnSubmit

Call Check(Document.MyForm.Company.Value,     "Please enter a company name.")
Call Check(Document.MyForm.Name.Value,     "Please enter a name.")

Sub Check(ByVal FieldValue, ByVal message)
        If FieldValue = "" Then
            MsgBox message, 8, Header
            validation = False
            End If
        End Sub

       


Other 7 submission(s) by this author

 

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this article(in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
8/28/2000 11:37:28 AMSteve Beesley

I agree that form field validation is very important, however you must remember that VBScript can only be run client side in Internet Explorer. This will not work in other browsers such as Netscape.
(If this comment was disrespectful, please report it.)

 
11/16/2000 1:28:52 PMsmartware

Do we really care about Netscape?
(If this comment was disrespectful, please report it.)

 
12/13/2001 12:40:06 PMdouglas.cranston@verizon.com

Enjoyed the article..

Question. What does the vbsample.asp code look like? Although it would appear that the code in your tabled description covers it, I found that entering my first name of
(If this comment was disrespectful, please report it.)

 
12/13/2001 12:42:55 PMDoug Cranston

Enjoyed the article..

Question. What does the vbsample.asp code look like? Although it would appear that the code in your tabled description covers it, I found that entering 5 or more characters cause no error message to appear as expected. Blank field does generate the error expected.

Would appreciate your comments and a chance to see the hidden vbsample.asp code.

Thanks inadvance.

Doug Cranston
douglas.cranston@verizon.com
(If this comment was disrespectful, please report it.)

 
4/18/2002 7:01:50 PMpaul_cormie

to answer smartwares question: i don't but my clients do care about NN.
(If this comment was disrespectful, please report it.)

 
5/15/2002 1:04:29 AMMuhammad Furqan

I love English people very much. Bcz they are very sincere with everyone. Well Can someone tell me the very basic site of ASP with download able source and tutorials . Please mail me back on furqanweb@yahoo.com
(If this comment was disrespectful, please report it.)

 
5/23/2002 3:32:42 PMMark

Won't the message box come up on the server side and not the client side? So the user never sees the error...like in the sample above.
(If this comment was disrespectful, please report it.)

 
6/7/2002 10:54:08 AMbob.berry@gte.net

could you also show how subroutine is invoked
(If this comment was disrespectful, please report it.)

 
7/8/2002 10:21:31 AMKarl Buckland

I found that this example returned no message boxes but eac time it went to a second page. This page informed me of my IP address and what I had typed, except in the case of the empty box when the page was entirely blank.


I'm using Internet Explorer 6 with Win2000. Do you think this makes a difference?
(If this comment was disrespectful, please report it.)

 
7/26/2004 4:57:35 PM

I found this sample for multiple validation very helpful. Thank you very much.

EnCry
(If this comment was disrespectful, please report it.)

 
10/16/2004 1:19:14 AM

sd
(If this comment was disrespectful, please report it.)

 
1/7/2006 9:42:36 AMmohseen

laude kiska code chori kiya,
mera code mere se miau

(If this comment was disrespectful, please report it.)

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
To post feedback, first please login.


 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | ASP/ VbScript Home | Site Home | Other Sites | Open Letter from Moderators | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997-2008 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.   Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.