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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation 1

Status
Not open for further replies.

morechocolate

Technical User
Apr 5, 2001
225
US
I have seen on sites where if there was an error in form validation there is a message displayed above or near the textbox, checkbox, etc. where the error occured, usually in bold red font.

When I have compared the before error (before submitting form) and after error source code I notice that the after error source code is slightly different than the before error source code. This leads me to believe that part of the form validation is being done on the server side. I am correct in my thinking?

In either case, how is this technique done? Is ASP the best way to do this?

Thanks
 
You can do this with ASP, whether this is how the sites you've seen have done it or not I couldn't say. How you would do it with ASP is this: Make your form page an ASP page. The form action should be the current page, so you are essentially reloading the same page when the submit button is clicked. Before any html on the page put your validation code.
Code:
<% 
for each field in request.form
    if request.form(field)<>&quot;&quot; then
    ' build response
    msgbody=msgbody & field & &quot;: &quot; & request.form(field) & vbcrlf
    else
    ' set a flag so we can continue with old page, highlighting missing fields
    flag=1
    end if
next
%>
If everything is ok you can do a redirect to the page that displays the thankyou message, if not you continue to reload the original page.
Code:
<%
if flag<>1 then 
   ' do mail routine (however you normally do it, we use JMail)
   ' redirect to thanks page
   response.redirect(&quot;thanks.html&quot;)
end if
%>
In front of the offending item/s you put something like the following:
Code:
<% if request.form(&quot;name&quot;)=&quot;&quot; then %><font color=&quot;red&quot;>Please fill in this field</font><% end if %>
This means that the field only gets highlighted when there is a field in the form collection called name and it is empty, ie when someone has submitted a form and the field was not filled in (which is what we want).

That should be enough to give you an idea of what is involved. Certain details may be incorrect or incomplete as I have just written all the code above from the top of my head and haven't tested any of it.

Hope it helps.
 
in pseudo code:

[EXAMPLE.ASP]


if request.form(&quot;fSubmitted&quot;) = &quot;yes&quot; then
' start validation
if someerror then
cErrMsg = &quot;This field is in error&quot;
else
response.redirect &quot;somewhereelse.asp&quot;
end if
end if


<form method=post action=example.asp>
<input type=hidden name=fSubmitted value=yes>
if cErrMsg <> &quot;&quot; then
response.write cErrMsg
end if
<input type=text>
<input type=submit>
</form>




br
Gerard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top