[thelist] help with form validation using ASP/VBScript

Brian Cummiskey Brian at hondaswap.com
Mon Feb 28 16:32:57 CST 2005


Pringle, Ron wrote:

> Ahh, makes sense now. And thank you for pointing out that it won't work in
> anything other than IE. Since our site has to be cross browser compatible, I
> guess I can forget validating it that way.
> 
> So it appears that I'm left with sending the form to a validation page?

server-side is the only TRUE way to validate a page.  VB can be off, JS 
can be off, etc.

another method i do is this:

on the page of your form:

<%
dim errorcount, errormsg
if isNumeric(Request("errorcount")) then
   errorcount = CInt(Request("errorcount"))
else
  errorcount = 0
end if

errormsg = Cstr(Request("errormsg"))

if errorcount > 0 then
	if errorcount = 1 then
		response.write "<div class='errorbox'><h6>There was " & errorcount & " 
Error in your Input:</h6>"	
	else
		response.write "<div class='errorbox'><h6>There were " & errorcount & 
" Errors in your Input:</h6>"
	end if
	response.write "<div style='text-align:left;'><ul>" & errormsg & 
"</ul></div></div>"
end if

%>

and set up your table cells like this:

<%
if request("errorfield1") then
	response.write "<td class='errorcell'>"
else
	response.write "<td>"
end if
%>

(assign the class with css to highlight)


This doesn't get called on first load because errorcount is less than 0.

now, when you submit the form to the checker.asp page, do something like 
this:


response.write "<div><form name='redirectme' action='form.asp' 
method='post'>" & vbcrlf

if request("field1") = "" then
		errormsg = errormsg & "<li>A <em>FieldNameHere</em> is required</li>" 
& vbcrlf
	errorcount = errorcount + 1
	response.write "<input type='hidden' name='errorfield1' value='" & 
request("field1")&"' />" & vbcrlf
end if

' put the rest of your check fields here ilke above....

if errorcount > 0 then
	%>
	<input type='hidden' name='errormsg' value='<%= errormsg %>' /> & vbcrlf
	<input type='hidden' name='errorcount' value='<%= errorcount %>' /> & 
vbcrlf
	</form></div>
	<%
	response.write "<script type='text/javascript'>" & vbcrlf
	response.write "redirectme.submit();" & vbcrlf
	response.write "</script>" & vbcrlf
else

response.write "</form></div>" & vbcrlf

this sets up your error list, and reidrects you back to the previous 
page, and generates the error list.

voila :)

this, too, though, relies on javascript for the re-direction.
you could use the response.redirect command, but the problem with that 
is that it doesn't POST the data- it simply forwards the browser on. 
i've yet to come up with a better solution, and frankly, diddn't have 
to, as it was an intranet application.

HTH

i'm going home for the day :)









More information about the thelist mailing list