Daniel M. Hendricks

Automatic Javascript Form Validation

This is a simple script I wrote to automatically create the code necessary to validate form fields using Javascript. The code makes it easy to create validation on your forms without having to manually code the Javascript each time.

The ASP Code

<%
Sub javascript_validate(strFields, strFunctionName)
   Dim strValidate

   If strFunctionName = "" Then strFunctionName = "validate"
   If strFields <> "" Then
   %>
      
   <%
   End If

End Sub
%>

Sample Form Usage




<%
'Usage: javascript_validate [list of varables and errors if blank], [name of validation function]
javascript_validate "fullName:Please enter your full name:age:You must enter your" & _
     "age.:form.gender.selectedIndex == 0:Please choose a gender.", "validateForm"
%>


Enter your name: Enter your age: Gender:

In this example, if the user clicks Submit without filling out some or all of the required fields, they will get a message like this:

Post a Comment

You must be logged in to post a comment.

Tip: Sign up for a free Gravatar to have a photo next to your comment! Your gravatar will follow you around when you post to blogs that support it, based on the e-mail address you use to post.