www.webdeveloper.com
Results 1 to 7 of 7

Thread: Server side validation - sample?

  1. #1
    Join Date
    Dec 2002
    Posts
    23

    Server side validation - sample?

    Hello

    I have a server side script which checks that form fields have been completed. If not, an error page is called pointing out that 'Name' (for example) must be completed, and invites the user to 'go back' and complete the fields correctly.

    However, I think we have all seen those error messages (normally in red type) next to the fields themselves - in other words, not another, separate asp file inviting the user to 'go back'.

    In fact, some of the better sites keep those fields, which have been completed properly, intact - the visitor doesn't need to complete them again because only the erroneous fields are highlighted.

    Has anybody on the forum any sample script (it needs to be server side, since I already have a couple of Javascripts on the page) which would serve my needs? It would need to redirect the visitor to another page if all form fields have been correctly completed.

    Thank you in anticipation.

    laTortuga

  2. #2
    Join Date
    Jan 2003
    Location
    Dundee, Scotland
    Posts
    1,367
    hi here is an example of using asp to validate your form. This is all done using 1 page. Hope this helps:

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

    <body>
    <%
    val = request.form("val")
    if val = "yes" then
    username = request.form("name")
    thesubject = request.form("thesubject")
    email = request.form("email")
    if username = "" then
    errorusername = "yes"
    end if
    if thesubject = "" then
    errorthesubject = "yes"
    end if
    if email = "" then
    erroremail = "yes"
    end if

    end if
    if val = "" or errorusername = "yes" or errorthesubject = "yes" or erroremail = "yes" then
    %>
    <form name="form1" method="post" action="validationexample.asp">
    <table width="100%" border="0" cellspacing="0" cellpadding="4">
    <tr>
    <td width="31%">Name:</td>
    <td width="69%">
    <input name="Name" type="text" id="Name" value="<%=username%>">
    <%
    if errorusername = "yes" then
    response.write "You didn't enter a user name"
    end if
    %>
    </td>
    </tr>
    <tr>
    <td>Subject:</td>
    <td>
    <input name="thesubject" type="text" id="subject" value="<%=thesubject%>">
    <%
    if errorthesubject = "yes" then
    response.write "You didn't enter the subject"
    end if
    %>
    </td>
    </tr>
    <tr>
    <td>Email Address:</td>
    <td>
    <input name="email" type="text" id="email" value="<%=email%>">
    <%
    if erroremail = "yes" then
    response.write "You didn't enter your email address"
    end if
    %>
    </td>
    </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="4">
    <tr>
    <td>
    <input type="submit" name="Submit" value="Submit">
    <input name="val" type="hidden" id="val" value="yes"></td>
    </tr>
    </table>
    </form>
    <%else
    response.write "Your form was completed with out errors"
    end if
    %>
    </body>
    </html>
    ----------------------------------

  3. #3
    Join Date
    Dec 2002
    Posts
    23

    Validation?

    Hello SWR!

    Many thanks for your post. I'm grateful.

    Can I ask, in the following line

    <form name="form1" method="post" action="validationexample.asp">

    is validation.asp the page called if/when the form is validated, that is, the form called when there are no errors in the form fields?

    laTortuga

  4. #4
    Join Date
    Jan 2003
    Location
    Dundee, Scotland
    Posts
    1,367
    hi,
    yes, the page for the form and the page for validation and the page if the form is validated are all on the one page.
    ----------------------------------

  5. #5
    Join Date
    Dec 2002
    Posts
    23

    Validation

    Hello SWR

    Thank you for your message.

    My mistake was not to call the file validationexample.asp. Now that I have, it seems to be working fine. Thank you.

    I now need to use something like:

    <% Response.Redirect "abcpage.asp" %>

    once the script has checked for errors and found none. I am concerned about where I put this, because I don't want the script to redirect to abcpage.asp until it has checked for all errors in the form. I think this means I need to turn off buffering also, otherwise the script will redirect the user anytime after the <HTML> tag.

    Can you help (again)?!

    laTortuga

  6. #6
    Join Date
    Jan 2003
    Location
    Dundee, Scotland
    Posts
    1,367
    hi,
    so you want to redirect after validation. Would this be to a normal page in the site or to a thank you page. If its to a thank you page then you don't need to redirect as this thhank you page is included in the code i gave you:

    <input name="val" type="hidden" id="val" value="yes"></td>
    </tr>
    </table>
    </form>
    <%
    else
    '---------------here is where you would put your thank you page. You can have a complete page here anything above this code will not be display if the form valitation came back as valid-------------------------------------------
    response.write "Your form was completed with out errors"
    end if
    %>
    </body>
    if you don't want to display a thank you message and you need to redirect to a different page then replace your thankyou message with the code you already have:

    <% Response.Redirect "abcpage.asp" %>
    like bellow:
    <input name="val" type="hidden" id="val" value="yes"></td>
    </tr>
    </table>
    </form>
    <%
    else
    '---------------here is where you would put your redirect code. If the form is valid then this line would be the only line that would run-------------------------------------------
    Response.Redirect "abcpage.asp"
    end if
    %>
    if you want to add variables to the querysting you do this:
    var1 = "test"
    Response.Redirect "abcpage.asp&var1="&var1&""
    ----------------------------------

  7. #7
    Join Date
    Dec 2002
    Posts
    23

    THANK YOU (AGAIN!)

    Hello SWR

    That's great, SWR. It works!

    I was just a bit concerned the page might try to redirect before checking the fields, but no, it's fine.

    Thanks a a lot.

    laTortuga

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