Validating Shipping Information During the Check Out Process


Introduction.  Now we need to develop an ASP that will be used to validate the shipping information that was inputted by the shopper.  The file will be called ValidateShipping.asp.  This ASP doesn't make sue of any stored procedures and is largely a processing script that relies on Shipping.asp for user interaction.

The ValidateShipping.asp.  The following ValidateShipping.asp will 

  • Check to make sure that appropriate information has been entered on the Shipping.asp
  • Accumulate an error message that will provide feedback to the shopper on the Shipping.asp.

Some of the major inputs that are validated are

  1. Name and complete address must be entered.
  2. The country must be selected and an appropriate state or province.
  3. A phone number must be entered
  4. An e-mail address is required and a bit of internal consistency validation for the presence of @ or . are implemented.

After having taken the shopper inputs and placed them in appropriate session variables, 

  • if an error is detected the shopper is sent back to Shipping.asp.
  • if no errors are detected then the shopper is sent on to the Payment.asp.
<%@ Language=VBScript %>

' Retrieve the submitted items from Shipping.asp
chrShipFirstName = request("chrShipFirstName")
chrShipLastName = request("chrShipLastName")
chrShipAddress = request("chrShipAddress")
chrShipCity = request("chrShipCity")
chrShipState = request("chrShipState")
chrShipProvince = request("chrShipProvince")
chrShipCountry = request("chrShipCountry")
chrShipZipCode = request("chrShipZipCode")
chrShipPhone = request("chrShipPhone")
chrShipEmail = request("chrShipEmail")

' Accumulating the error message and checking for blank entries
' Check to ensure a first name was entered.

if chrShipFirstName = "" then

strError = strError & "Invalid first name<BR>"

end if

' Check to ensure a last name was entered.
if chrShipLastName = "" then

strError = strError & "Invalid last name<BR>"

end if

' Check to ensure an address was entered.
if chrShipAddress = "" then

strError = strError & "Invalid address<BR>"

end if

' Check to ensure a city was entered.
if chrShipCity = "" then

strError = strError & "Invalid city<BR>"

end if

' Check to see if a US country was entered.
if chrShipCountry = "US" then

' If the state field is empty then build the appropriate error message.
if chrShipState = "" then

strError = strError & "Invalid state<BR>"

end if


' If it is an international country is entered then a province must be entered.
if chrShipProvince = "" then

strError = strError & "Invalid province<BR>"

end if

end if

' Check to see if a country was entered.
if chrShipCountry = "" then

strError = strError & "Invalid country<BR>"

end if

' Check to ensure a zip code was entered.
if chrShipZipCode = "" then

strError = strError & "Invalid zip code<BR>"

end if

' Check to ensure a phone number was entered.
if chrShipPhone = "" then

strError = strError & "Invalid phone number<BR>"

end if

' Next we validate the email address. 
' The email address must have a @ symbol or period.

if (instr(1, chrShipEmail, "@") = 0) or (instr(1, chrShipEmail, ".") = 0) then

strError = strError & "Invalid email address<BR>"

end if

' Check to ensure that only one of the state or province fields are filled in.
if (chrShipState <> "") and (chrShipProvince <> "") then

strError = strError & "You can not fill in both the " & _
"state and province fields<BR>"

end if

' Copy the shipping values to session variables
session("chrShipFirstName") = request("chrShipFirstName")
session("chrShipLastName") = request("chrShipLastName")
session("chrShipAddress") = request("chrShipAddress")
session("chrShipCity") = request("chrShipCity")
session("chrShipState") = request("chrShipState")
session("chrShipProvince") = request("chrShipProvince")
session("chrShipCountry") = request("chrShipCountry")
session("chrShipZipCode") = request("chrShipZipCode")
session("chrShipPhone") = request("chrShipPhone")
session("chrShipEmail") = request("chrShipEmail")

' Check to see if an error was generated and route the shopper accordingly
if strError <> "" then

' Store the error in a session variable and then direct the shopper back ot the shipping page.
session("Error") = strError
Response.Redirect "shipping.asp"


' All of the data is correct, so we copy the shipping data
' to billing variables to default the data on the payment form.
' That way the user does not have to retype their billing information
' if it is the same.

session("chrBillFirstName") = request("chrShipFirstName")
session("chrBillLastName") = request("chrShipLastName")
session("chrBillAddress") = request("chrShipAddress")
session("chrBillCity") = request("chrShipCity")
session("chrBillState") = request("chrShipState")
session("chrBillProvince") = request("chrShipProvince")
session("chrBillCountry") = request("chrShipCountry")
session("chrBillZipCode") = request("chrShipZipCode")
session("chrBillPhone") = request("chrShipPhone")
session("chrBillEmail") = request("chrShipEmail")

' Send the user to the payment page.
Response.Redirect "Payment.asp"

end if



This ASP is one of the longest we have developed so far.  While the logic isn't that complicated, there is a lot of HTML code to make sure things are available in the form and to check whether controls have been filled in appropriately.