Click to See Complete Forum and Search --> : ASP Form Validation


myself1
03-19-2008, 08:12 AM
Hi all :o .
I am trying to get users to fill out all the required fields on my APS form, as follows:


<!--Input Details Here -->
<form id="bro" name="addfrm" method="post" action="matconfirm.asp">
<INPUT TYPE="hidden" NAME="BrDate" VALUE="<%=DATE()%>">
<span class="stress">Fill in ALL fields</span>

<fieldset>
<p><label for="BrfName">Name:</label><input name="BrfName" type="text" class="txt" id="BrfName" maxlength="15"></p>
<p><label for="BrSurname">Surname:</label><input name="BrSurname" type="text" class="txt" id="BrSurname" maxlength="15" /></p>
<p><label for="BrAge">Age:</label><select name="BrAge">
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
</select>
</p>
<p><label for="BrEmail">Email Address:</label><input name="BrEmail" type="text" class="txt" id="BrEmail" maxlength="35" /></p>
<p><label for="BrPhone">Phone:</label><input name="BrPhone" type="text" class="txt" id="BrPhone" maxlength="20" /></p>
</fieldset>

<fieldset>
<p>
<label for="BrTown">Town/City:</label>
<input name="BrTown" type="text" class="txt" id="BrTown" maxlength="20" />
</p>
<p>
<label for="BrOrigin">Ethnicity:</label>
<input name="BrOrigin" type="text" class="txt" id="BrOrigin" maxlength="20" />
</p>
<p>
<label for="BrNational">Nationality:</label>
<input name="BrNational" type="text" class="txt" id="BrNational" maxlength="15"/>
</p></fieldset>

<input type="submit" name="btnSubmit" id="btnSubmit" value="Register Me Please" class="btn" />
</p>
</form>


What do I need to add so the users have to fill in such data.
Hope to hear. TIA. :)

yamaharuss
03-19-2008, 10:21 AM
If validating server-side you will need to add the following to matconfirm.asp

You can handle the errors in many ways, this is one example.

' retrieve fields
BrfName = TRIM(Request.form("BrfName"))
BrSurname= TRIM(Request.form("BrSurname"))
' etc... do with all form fields

'now validate
If BrfName = "" then
Response.write("First Name required. Please go back.")
Response.end
End If

If BrSurname = "" then
Response.write("Surname required. Please go back.")
Response.end
End If

' etc...

myself1
03-19-2008, 11:50 AM
Ok. Thank you for your input...appreciated :)

If there is a blank field, will the user be automatically returned back to the form? :o

yamaharuss
03-19-2008, 02:45 PM
Ok. Thank you for your input...appreciated :)

If there is a blank field, will the user be automatically returned back to the form? :o

You may want to implement client-side Javascript validation and use the server-side as a backup. That will give you the best protection.

Javascript will keep you on the form with no need to "go back". You can implement session validation for the ASP code, something like:


'now validate
isError = False
If BrfName = "" then
isError = True
Session("BrfNameError") = True
End If

If BrSurname = "" then
isError = True
Session("BrSurnameError") = True
End If

' etc...
If isError then
Response.redirect("myformpage.asp")
Response.end
End if


Then back on your form page:

If Session("BrfNameError") then Response.write("First Name is required<BR>")
If Session("BrSurnameError") then Response.write("Surname is required")
etc..

You can even take it further by adding each form value to a session variable and populate the form fields with those values to prevent having to complete the form all over again.