Click to See Complete Forum and Search --> : validation


jrthor2
04-16-2003, 01:59 PM
I have a form that submits to the following page:

<%
'Set the response buffer to true as we maybe redirecting
Response.Buffer = True

If Trim(Request.Form("LastName")) = "" Then
Response.write "<b>Please enter a Last Name.</b><br>"
End If

If Trim(Request.Form("FirstName")) = "" Then
Response.write "<b>Please enter a First Name.</b><br>"
End If

Set conn = server.createobject("adodb.connection")
DSNtemp="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("members.mdb")
conn.Open DSNtemp

SQLstmt = "INSERT INTO MemberList (LastName,FirstName,Address1, Address2,CityState,Zip)"
SQLstmt = SQLstmt & " VALUES ('" & request.form("LastName") & "','" & request.form("FirstName") & "','" & request.form("Address1") & "','" & request.form("Address2") & "','" & request.form("CityState") & "','" & request.form("Zip") & "')"
%>
<%

conn.execute(SQLstmt)

conn.Close
Set conn = nothing
Set SQLstmt = nothing
Response.Redirect("index.asp?name=" & Request.Form("name"))
%>

If I don't enter a last name or first name, it still submits the row, it doesn't give me the error that you must enter that field. What is wrong here?

Thanks

jpmoriarty
04-16-2003, 02:36 PM
i dont know that much asp, but one of your problems is that altough you program to produce a statement (ie please enter a last name), you then dont insert anything to stop the program running the rest of the code - hence it will (when it's done right) say "please enter a last name", but quite happily go off and run the SQL anyway - so you need to stop it doing that.

jrthor2
04-16-2003, 02:37 PM
I kind of figured that part out, but how do you stop it?

Nicodemas
04-16-2003, 04:32 PM
Extend your If ... Then statement to an If ... Else statement.



<%
'Set the response buffer to true as we maybe redirecting
Response.Buffer = True

If Trim(Request.Form("LastName")) = "" Then
Response.write "<b>Please enter a Last Name.</b><br>"
End If

If Trim(Request.Form("FirstName")) = "" Then
Response.write "<b>Please enter a First Name.</b><br>"
Else

Set conn = server.createobject("adodb.connection")
DSNtemp="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("members.mdb")
conn.Open DSNtemp

SQLstmt = "INSERT INTO MemberList (LastName,FirstName,Address1, Address2,CityState,Zip)"
SQLstmt = SQLstmt & " VALUES ('" & request.form("LastName") & "','" & request.form("FirstName") & "','" & request.form("Address1") & "','" & request.form("Address2") & "','" & request.form("CityState") & "','" & request.form("Zip") & "')"

conn.execute(SQLstmt)

conn.Close
Set conn = nothing
Set SQLstmt = nothing
Response.Redirect("index.asp?name=" & Request.Form("name"))
End If
%>

jpmoriarty
04-17-2003, 02:39 AM
well technically that wont actually work since if you leave off the first name it will still process the sql. In PHP there's an exit(); function which stops the script from running - i presume there's something similar in asp that just stops the whole kaboodle (which is what you'd want in each of your if's at the start).

I'd suggesting making the 2nd if an elseif - that will make it run better, but it's only going to give you one error (ie no first name or no last name, but not both) so you may want to include another clause when both first name is blank and 2nd name is blank, and then post the dual error - if you're that bothered that is,

jrthor2
04-17-2003, 08:38 AM
Can someone please help me do this validation?

khaki
04-17-2003, 07:28 PM
Hi jrthor2...

Try placing this before your end if to stop the code from proceeding:

exit sub

I beleive that this will halt any further code from running so that you can trap
for getting your fields properly completed.

;) k

jrthor2
04-18-2003, 08:34 AM
Thanks for all your help, that worked!!