PDA

View Full Version : how to validate form


jarv
01-06-2010, 10:30 AM
I would like to validate my Add form, is there a way of putting my fiorst bit of code inside my second bit of code?

I need to say something like request.form("Add") and then request.form("size")?!

<!-- Form validation -->
<%
IF request.form("size") = "" THEN
response.write("Please enter a valid size number")
END IF
IF request.form("colour") = "" THEN
response.write("Please enter a valid colour")
END IF
IF request.form("stock") = "" THEN
response.write("Please enter a valid stock number")
END IF
%>
<!-- END Form validation -->



<%
If Request.Form("add") = "Add" Then
sql = "INSERT INTO tblProductOptions (ProductID, `Size`, Colour, Stock, ComingSoon) VALUES (" & _
"" & Strip(Request.Form("id")) & ", " & _
"" & Strip(Request.Form("size")) & ", " & _
"'" & Strip(Request.Form("colour")) & "', " & _
"" & Strip(Request.Form("stock")) & ", " & _
"" & iif(IsNumeric(Strip(Request.Form("ComingSoon"))), 1, 0) & "" & _
");"
Set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = MM_CON_Database_STRING
conn.open
response.write sql
conn.Execute sql
Set conn = Nothing
Response.Redirect("index.asp?id=" & Request.Form("ID"))
End If

Old Pedant
01-06-2010, 08:28 PM
<%
If Request.Form("add") = "Add" Then
oops = ""
id = Strip(request.form("id"))
If id="" OR Not IsNumeric(id) Then oops = oops & "<br/>Please enter a valid product id"
size = Strip(request.form("size"))
If size="" OR Not IsNumeric(size) Then oops = oops & "<br/>Please enter a valid size number"
colour = Strip(request.form("colour"))
If colour="" Then oops = oops & "<br/>Please enter a valid colour"
stock = Strip(request.form("stock"))
If stock="" OR Not IsNumeric(stock) Then oops = oops & "<br/>Please enter a valid stock number"
If oops Then
Response.Write "Please hit BACK then correct the following errors then try again:" & oops
Response.End
End If
If IsNumeric(Trim(Request("comingSoon"))) Then cs = 1 Else cs = 0
sql = "INSERT INTO tblProductOptions (ProductID, `Size`, Colour, Stock, ComingSoon) " _
& " VALUES (" & id & "," & size & ",'" & colour & "'," & stock & "," & cs & ");"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open MM_CON_Database_STRING ' one statement instead of two
response.write "DEBUG SQL: " & sql & "<hr>"
conn.Execute sql
Set conn = Nothing
Response.Redirect "index.asp?id=" & id ' no parens needed/wanted
End If

jarv
01-07-2010, 10:18 AM
thanks, I now get:

Microsoft VBScript runtime error '800a000d'

Type mismatch: '[string: "<br/>Please enter a "]'

/paddycampbell.www.co.uk/www/office/additems/content.asp, line 23

Old Pedant
01-07-2010, 06:59 PM
That URL doesn't work.

But probably not important.

Which line is line 23???

Maybe show the actual code? I don't see how you get that error from my code.

Did you maybe already have a variable named "oops"???

jarv
01-08-2010, 09:16 AM
Line 23: If oops Then

Old Pedant
01-08-2010, 07:00 PM
Oh! Sorry! So obvious once you tell me what line.

Been working in JS and C++ too much.

Should be

If oops <> "" Then