VB Script - Form Validation Routine

Does anyone have a decent generic form validation script they want to share?  I am looking for a script I can call for each field in my form where I can specify a validation type (Phone, Email, URL, generic text, etc) and set a required/not required flag on a field by field basis.  Please include the script and the code to call it.

Thanks a ton, E
Who is Participating?
WebsiteNationConnect With a Mentor Commented:
You have 2 options. You can either submit the form to itself:
(Assuming Submit button is labeled Submit)
<% If Request.Form("Submit") = "Submit" Then
    If  error testing code...
    Process error messages...
    Send Form Script
    End If
End If %>

Or submit the form to a confirmation page that will test for the errors.
Check the following:

Javascript Solution: http://www.yaromat.com/dw/?ex=Check%20Form%20MX

EWilson12Author Commented:
wee free plugins, nice plugin but unfortunately I am hesitant to use pop ups (win XP SP2 and all) to notify the user that the form is incorrect.  I would rather repop the form page with labels for all incorrect fields in a BOLD RED font with a short description message at the top of the page informing the user to check all the fields that are marked in this manner for errors.  I did however install the plugin for future use so 50pts for your assist once this question is answered.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

EsopoConnect With a Mentor Commented:
Quoting myself from a different Q:

You need to decide wether you want this validation done on the server or client side. (or both)

On the client side you can have the validation pop a message before sending the form. On the server you have more control and reliability but the user must wait until the new page loads telling they missed a spot.

For client side validation you'll be using probably javascript, with something like what Steve suggested. For server side validation you need your host server to have some form of cgi scripting like ASP or PHP.

This is a good link for understanding the main reasons for using Server Side valdiation:

Here you can find great ASP scripts for many types of validations:

Best regards,

I agree. Then use ASP validation. It will be better for you.


<% If Len Request.Form("field") = 0 Then Response.Write("Error") End If %>
Opps, Correction:

<% If Len(Request.Form("field")) = 0 Then Response.Write("Error") End If %>
EWilson12Author Commented:
Wouldn't a combination of both be the best?  1st Validate on the page itself so the user has immediate feedback w/o having to reload the page, then validate with an asp page for additional control and reliability?
Yes, I use both methods. It is really up to you. I use the javascriopt mehtods for non-critcal stuff. If I need a gauranteed validation I will use ASP. Once you start writing your own ASP validation, you will get the hang of it. I like it much better then javascript which can get very messy at times. :)

A good place to learn about VBScript and ASP is here: http://www.w3schools.com/
EWilson12Author Commented:
wouldn't a response.write fire before a user even gets to the field, when the page is initially loaded? Can I use an in the page script that gets called when the forms submit button is pressed?  If I can do it that way can someone show me how to modify the following code to call the on page script instead of submitting the form?

<form name="form1" method="post" action="applied.asp">
      <input type="text" name="textfield">
      <input type="submit" name="Submit" value="Submit">

Also, If I do call a script can someone show me an example of posting the form data to the next page from within the script seeing how my submit button won't do this for me once modified to call the script.

I am thinking of including some error text in my page that initially is the same color as the background then if the validation rules are not met the script will just change the text color and refresh the page.
EWilson12Author Commented:
Sorry for the delay but it took me awhile to get on track, My final result was no client side validation at all and I ended up doing exactly what Websitenation suggested by having the form call itself, actually I ended up with 3 pages for the application:

index_apply.htm was just used to collect the data and submit it to index_apply.asp
index_apply.asp was used for validation and data correctio and either calls itself until validation is passed
index_applied.ap is called when all validation has been met and contains the CDONTS for emailing the application.

final result was:


actually ran accross something new (thanks to BubbaisBest for his input on another question i had) for me to get the form data over to my applied page w/o the use of a form action and that was:


worked really well.

Thanks for all the info, E
Gald to help :)

Just one last suggestion:
Would you please turn those nasty & disturbing table borders off?
EWilson12Author Commented:

I'll consider it.
Great, glad that worked! True, you don't need javascript if you submit the form to itself and use ASP. But, sometimes I will use both. I like that Javascript gives an immediate message and saves a trip to the server. But, I will also backup the form with an ASP validation for anything that MUST be submitted. This is more important for database applications.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.