Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Field validation using ASP/VBScript

Status
Not open for further replies.

madHatter1

Technical User
Feb 27, 2003
54
GB
Hello

I have a form which invites the user to input details such as name, email address, etc.

The form, which is CDONTS-based, works fine, but I have no script which assists in validating field input.

I prefer not to use the bulky Javascript 'alert' boxes, but Iam looking for a script which, if the site visitor forgets to type his name (for example) in the relevant form field, then a message, something along the lines of 'Please type in your name', appears above the name field of the form itself.

If the visitor has tupes in nothing, then 2/3/4 error messages should appear simultaneously above (or alongside) their respective fields. We might end up with, for instance, the following:

Please type in your name
NAME FIELD FIELD HERE

Please type in your email address
EMAIL ADDRESS FIELD HERE

Please type in your message
MESSAGE FIELD HERE

Of course, if there all field have been correctly completed, the form should be submitted without any error message appaering above any of the fields.

I have seen this before on the Web, and wondered how it is done.

Thank you for any replies.

Hatter
 
you'd have to make the form action point to the same page.

Asp would then use Request.Form to validate each input.

If an input is incorrect then set error flag to 1 and display the error you want

If you reach the bottom of the page and error flag is still 0, then response.write some javascript that automatically changes the action of the form to whatever you want and submits it.
 
I think that you're talking about the ASP.NET Validator Control here. Play around with this and use a variation. You'll need to format your label (using inline style) so it doesn't look like a text box. I'm sure you'll be able to figure it out with this to go on:

<html>
<head>
</head>
<body>
<form name=&quot;form1&quot;>
<input type=&quot;text&quot; name=&quot;txt1&quot;><br>
<input type=&quot;label&quot; name=&quot;lbl1&quot; style=&quot;visibility:hidden;&quot;><br>
<input type=&quot;submit&quot; name=&quot;btnsubmit&quot; value=&quot;submit&quot; OnClick=&quot;Validate()&quot;>
</form>
</body>
</html>
<script language=&quot;vbscript&quot;>
Sub Validate()
if len(form1.txt1.value)=0 or form1.txt1.value=&quot;&quot; then
form1.lbl1.style.visibility=&quot;visible&quot;
form1.lbl1.value=&quot;Enter Something Here&quot;
window.event.returnvalue=false
exit sub
end if
End Sub
</script>
 
Nevermoor's plan is a valid one. Requires a trip to the server though and a seperate error value that the asp code above each text box will have to recognize in order to show/not show the message. More complicated, more coding. I use it often though.
 
If you want to do it client side (certainly simpler), then something like Veep's method would work fine (and avoid messy alert boxes).

You can, of course, use javascript and spans or divs to accomplish the same thing. The only thing is that there is no need to play with the visibility if you are putting text into the label during validation. If you want the error message hard coded, then you'd need to take the visible/invisible steps.

Cheers,
-Nm
 
The reason that I did the visible/hidden and form1.lbl1.value=&quot;whatever&quot; is because we're suppose to be teaching people to fish, right? You know what you'd do in a given situation and so do I. This person is asking for a simulation of a .NET control in a Classic ASP Forum so I threw him a fishing pole and said &quot;play with this&quot;.

Let's make [cheers] not [swords]
 
something like this will work (ive kept is basic so you get the jist of it easily)

[form page]
<form action=validateform.asp method=post>
<input type=text name=text1>
<input type=submit>
</form>

[validateform.asp]
<%
if request.form(&quot;text1&quot;) = &quot;&quot; then
' If text1 was left empty the display message
response.write &quot;Please enter you name&quot;_
& &quot;<a href='javascript:history.back()'>Back</a>&quot;
end if
%>

OR

[form.asp]
<form action=form.asp method=post>
<%
if request.form(&quot;submit&quot;) = True then
' Will only go here if the form has been submitted
if request.form(&quot;text1&quot;) = &quot;&quot; then
' if text1 was left empty the we go here
response.write &quot;Please enter you name&quot;
nosave = 1 ' so the results arent saved
end if
end if
%>
<input type=text name=text1>
<input type=hidden name=submit value=true>
<input type=submit>
</form>
<%
if nosave <> 1 then
'Save results
end if
%>

thats 2 basic methods ive used before that you can consider

I can't be bothered to have a sig!
 
not trying to start any [cannon]

Just making sure he understands whats going on.

Like I said, server side is certainly cleaner (and doesn't require a relod -> saves server resources)

[peace]
-Nm
 
I wish i could use [morning] as an excuse, but that was just me being an idiot
 
hello

many thnaks. I've saved all your replies and will work on them!
hatter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top