www.webdeveloper.com
Results 1 to 5 of 5

Thread: Form validation help

  1. #1
    Join Date
    May 2008
    Posts
    7

    Form validation help

    Hello all,

    Before I start I am going to admit I know very little about asp coding and am looking to amend my code so it has form validation. I want the validation to require all fields are completed, a correct email address loaded and I want the code to go to a specific URL if it succeeds or fails rather than a line of text that is current displays. My current code is:

    <% @LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>


    <%

    Set Mail = Server.CreateObject("SMTPsvg.Mailer") 'create an Asp mail component.
    Mail.FromName = "Feedback"
    Mail.FromAddress= Request.Form("email")
    Mail.RemoteHost = "***remote_host_url***" ' The mail server you have to use with Asp Mail
    Mail.AddRecipient "alliancepoliceforce.co.uk", "webmaster@alliancepoliceforce.co.uk"
    Mail.Subject = "Website - Info Request"
    Dim strBodyText
    strBodyText = Request.Form("Feedback")
    strBodyText=strBodyText & Request.Form("name") & vbcrlf
    strBodyText=strBodyText & Request.Form("email") & vbcrlf
    strBodyText=strBodyText & Request.Form("comments")
    Mail.BodyText=strBodyText
    if Mail.SendMail then
    Response.Write "Your mail has already been sent..."
    else
    Response.Write "Mail send failure. Error was " & Mail.Response
    end if
    Set Mail = Nothing
    %>


    </body>

    </html>

    My question is how easy would it be to amend the code and where do I start? Any help would be greatly appreciated.

    Thank you

  2. #2
    Join Date
    Jan 2008
    Location
    Florida
    Posts
    1,227
    First, you should declare your form values ahead of your mail function, then validate them. You can do some further validation than exampled but you get the idea.

    Code:
    <% @LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    
    
    <%
    
    strName = TRIM(Request.Form("name"))
      If strName = "" then
        response.write("Please provide your name. Go Back.")
        response.end
      End If
    strEmail = TRIM(Request.Form("email"))
      If strEmail = "" then
        response.write("Please provide your email address. Go Back.")
        response.end
      End If
    strComments = TRIM(Request.Form("comments"))
    
    
    Set Mail = Server.CreateObject("SMTPsvg.Mailer") 'create an Asp mail component.
    Mail.FromName = "Feedback"
    Mail.FromAddress= Request.Form("email")
    Mail.RemoteHost = "***remote_host_url***" ' The mail server you have to use with Asp Mail
    Mail.AddRecipient "alliancepoliceforce.co.uk", "webmaster@alliancepoliceforce.co.uk"
    Mail.Subject = "Website - Info Request"
    Dim strBodyText
    strBodyText = Request.Form("Feedback")
    strBodyText=strBodyText & strName  & vbcrlf
    strBodyText=strBodyText & strEmail  & vbcrlf
    strBodyText=strBodyText & strComments
    Mail.BodyText=strBodyText
    Mail.SendMail
    
    response.redirect("ThankYou.asp")
    response.end
    Set Mail = Nothing
    %>
    
    
    </body>
    
    </html>

  3. #3
    Join Date
    May 2008
    Posts
    7
    Thank you for your response. I have added the code in as suggested and it comes back with the relevant form validation. However, is there anyway I can do it so that it just appears on the same page. So for example, if I had just completed the form and click send, rather than just go to a white screen with 'You need to fill in your email' is it possible to have it just appear on the same page, maybe just above the fields. Also, how do I amend the code to go to a specfic URL once the forms have been successfuly submitted.

    So if the form submits correctly it goes to:
    http://www.alliancepoliceforce.co.uk...n/success.html

    And if it fails it goes to:
    http://www.alliancepoliceforce.co.uk...on/failed.html

  4. #4
    Join Date
    Jan 2008
    Location
    Florida
    Posts
    1,227
    Well.. if the validation fails and you want to send it to failed.html, there is no need to write the error message on the same page.

  5. #5
    Join Date
    May 2008
    Posts
    7
    Ok as you can see I am new to all this...lol.

    The way I wanted it to go is that the validation would verify that all fields had been completed and that the email entered was valid. Once the user has then clicked on submit they are redirected to http://www.alliancepoliceforce.co.uk...n/success.html

    If they have not filled in all the forms correctly how would I do it so that it redirects to failed.html but then states specfically which fields they have failed to complete accurately.

    I guess I need to know how to change the code so it redirects to those pages and then how to add error message to the failed.html when it does.

    By the way before continueing just want to say thank you for your help on this.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles