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

New to ASP, IfElse Troubles

Status
Not open for further replies.

internetguy

Technical User
Nov 22, 2003
57
US
ok, I have a contact form that needs validation. Basically, if an input field is blank=report error, else move on to next input field. The only problem is that it is not moving onto the next input field to check. Here is my code.

<%
If Request.Form<>"" Then
If Request.Form("email")="" Then
Response.Write("You did not enter an email, please do so")
Response.Write("<br><a href='contact.asp'>Return</a>")
ElseIf Request.Form("subject")="" Then
Response.Write("You did not enter a subject address, please do so")
Response.Write("<br><a href='contact.asp'>Return</a>")
ElseIf Request.Form("message")="" Then
Response.Write("You did not enter a message, please do so")
Response.Write("<br><a href='contact.asp'>Return</a>")
Else
Set myMail=CreateObject("CDO.Message")
myMail.Subject=Request.Form("subject")
myMail.From=Request.Form("email")
myMail.To=Request.Form("james.devan@gmail.com")
myMail.TextBody=Request.Form("message")
Request.Write("Your message has been sent, thank you")
End If
 
What are u trying to check in this first line...

If Request.Form<>"" Then


-L
 
I am used to php, and in php you can use isset() to test if the submit button was pressed. I tried doing that in ASP where if the entire form was blank then it would load the form, and if it was filled in then it would proceed to check the inputs. Like I said, I am VERY new to ASP =P
 
fastest bulk way to do a check of input elements is :
Code:
CheckFields = Array("email","address","phone","LastName")
ErrorMsg = ""
for each item in CheckFields
  If Request(item) = "" Then
     ErrorMsg = ErrorMsg & item & " was blank, please enter a value<br>"
  End If
Next

If ErrorMsg = "" then
  Set myMail=CreateObject("CDO.Message")
  myMail.Subject=Request.Form("subject")
  myMail.From=Request.Form("email")
  myMail.To=Request.Form("james.devan@gmail.com")
  myMail.TextBody=Request.Form("message")
  Request.Write("Your message has been sent, thank you")
Else
  Response.Write "<font color=red>" & ErrorMsg & "</font><br>"
  Response.Write("<br><a href='contact.asp'>Return</a>")
End If

in summary, you make an array of the elemets to check, you check the request values for those items, you can also add conditionals in the loop for specific ones like :
If request(item) <> "" and item = "email" then
..<check for proper email syntax>..
etc..

then it looks if the errormsg is empty, if there's any errors, it's populated, otherwise it's made it through the validation without a fault, so it's ok to do the email, otherwise it spits out the error and a link back

also your email code seems to be missing a .send and also you're not clearing the mailer object when done, might want to add those in.

[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
 
You can use
Code:
If Request.Form("NameOfYourSubmitButton") <> "" Then
 
I don't get that, shouldn't it always be blank because it isn't a text or password string? I hate microsoft servers!
 
This is a vary simple validation and I think that common practice was doing validation on client with javascript?
 
internetguy

Most submit buttons are called submit, the value of the submit is the actual name on the button...ie: "Save My Information" so you can check if the form is submit via the button.

If isEmpty(Request.Form("submit")) then

'# form is not submited
Else
'# Form submited

End If


I am not a php guru but you can use the same method with php I blieve.

- Jason

www.sitesd.com
ASP WEB DEVELOPMENT
 
Ok, that makes sense. PHP uses names for each input field instead of the value in the field. Thanks for the help, this makes life much easier now that this was cleared up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top