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

form validation via arrays in elegant way 1

Status
Not open for further replies.

tester321

Programmer
Mar 13, 2007
150
CA
Hi i need to do some server side validation of a few form fields from request.form. I'm trying to build the statement in an elegant way if the request.form is empty, so i can response write, and split the message back to the user, this is what i have, any help appreciated in making it elegant. Thanks!

Session("mess")=""
Dim item

''' below set array to request.form values
Dim arrChk(6)
arrChk(0)=Trim(strGuestName)
arrChk(1)=Trim(strPhoneNumber)
arrChk(2)=Trim(strCity)
arrChk(3)=Trim(strStateProvince)
arrChk(4)=Trim(strCountry)
arrChk(5)=Trim(strCreditCardNumber)
arrChk(6)=Trim(strCreditCardExpiry)

'' below the message to cancatenate into a string to later
''split for the user to read

Dim arrMess(6)
arrMess(0)="Name is a required field."
arrMess(1)="Phone Number is a required field."
arrMess(2)="City is a required field."
arrMess(3)="State / Province is a required field."
arrMess(4)="Country is a required field."
arrMess(5)="Value Card is a required field."
arrMess(6)="Value Card Date is a required field."

For i=0 to 6

If arrChk(i)="" Then Session("mess") = Session("mess") & "|" & arrMess(i)

Next

If Session("mess")<>"" Then Response.Redirect("page1.asp")
 
Instead of 2 arrays, have a single two dimensional array

The first array dimension holds the field English names

The second array dimension holds the submitted values

Also you don't want to mess with session variables on this, just return the bad fields in the redirect querystring

Then the page1.asp will parse the querystring and report the message to the user.
Code:
<%
Dim arrChk(1,6)
arrChk(0,0)="GuestName"
arrChk(0,1)="PhoneNumber"
arrChk(0,2)="City"
arrChk(0,3)="StateProvince"
arrChk(0,4)="Country"
arrChk(0,5)="CreditCardNumber"
arrChk(0,6)="CreditCardExpiry"
 
arrChk(1,0)=Trim(Request("strGuestName"))
arrChk(1,1)=Trim(Request("strGuestName"))
arrChk(1,2)=Trim(Request("strGuestName"))
arrChk(1,3)=Trim(Request("strStateProvince"))
arrChk(1,4)=Trim(Request("strCountry"))
arrChk(1,5)=Trim(Request("strCreditCardNumber"))
arrChk(1,6)=Trim(Request("strCreditCardExpiry"))

Dim i
Dim strMissing
For i = LBound(arrChk,2) to UBound(arrchk,2)
  If arrChk(1,i)="" Then 
    strMissing = strMissing & arrChk(0,i) & ","
  End if
Next

'Remove trailing comma
strMissing = Left(strMissing, Len(strMissing) - 1)

If strMissing <> "" Then 
  Response.Redirect "page1.asp?Missing=" & strMissing
End If
%>



Then on page1.asp something like this:[tt]
if Request("Missing") <> "" then
Response.Write "The following fields are required: " & Request("Missing")
end if[/tt]




ASP is so, uh, well. It is so sloppy. There is always an urge to write something more elegant. The problem is that it usually comes with increased complexity that is more difficult for the next guy to maintian.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top