Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 28

Thread: Form validation

  1. #1
    Regular Coder
    Join Date
    Jan 2003
    Posts
    148
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form validation

    Hiya

    I wanna have my form validated before the data goes to the database. But I have so many if...else statements where I wanna connect to the database after else.

    It doesn't seem really nice using many if...then...else so is there any other way of keeping them in one if statement?

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Well you could put them all (or some) in one if...then statement.
    You could do something like:

    if str1="" OR len(str2)<6 Then
    'Error

    End If

    Doing the above mentioned method you might be able to condense some of your if...then statements.
    OracleGuy

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    putting them inside a single IF statement would make it inefficient since all of the conditions will be checked regardless of the value of each condition (true or false). Unlike javascript where in a compound OR condition, once the first condition evaluates to true, the rest of the conditions will not be check anymore since the output of the whole condition will become true. The same is true with the compound AND condition, once the first condition evaluates to false, the rest of the conditions will not be evaluated anymore, since the whole condition will become false. But this is not the case for vbscript. In VB.NET, they introduced new (funny) operators such as OrElse and AndAlso
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Interesting glenngv. I didn't know that.

    I haven't had a chance to mess around with VB .net yet... its "on the list."

  • #5
    Regular Coder
    Join Date
    Jan 2003
    Posts
    148
    Thanks
    0
    Thanked 0 Times in 0 Posts
    do u reckon I better do the validation with javascript then? I wonder what u think the advantages of using vb script or javascript in form validations?

    Thanks

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by pinkcat_02
    do u reckon I better do the validation with javascript then? I wonder what u think the advantages of using vb script or javascript in form validations?

    Thanks
    The advantage of doing validation with javascript is to reduce the turn around on errors, the user doesn't have to go all the way to the server to get a reply. The disadvantage is that you still need to have server side validation because some people don't have javascript enabled and you don't want garbage to get into your database (it's better to prevent it than trying to remove or deal with it). If you want to reduce the complexity of the validation try using some functions to perform repeated tasks or to handle exceptionally complex checks. You can even make the whole validation check into a function (which can call other functions) so the processing of the form isn't cluttered with the validation code.

  • #7
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    You also could put your validation code into an include to help reduce clutter on form processing part of the page.

  • #8
    Regular Coder
    Join Date
    Jan 2003
    Posts
    148
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can u please give me an example of what you have suggested oracleguy? I think it will make it more clear.

    Thanks

  • #9
    Regular Coder
    Join Date
    Jan 2003
    Posts
    148
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <%
    Session("FName")=Request("FirstName")
    Session("LName")=Request("LastName")
    Session("CompletionDate")=Request("SendDate")

    'HERE IS ONE WAY OF CHECKING FOR AN EMPTY TEXT BOX
    if not len(Request("FirstName")) > 0 then
    Session("badFirstName")="T"
    Session("Errors")=Session("Errors") + 1
    end if

    'AND HERE IS ANOTHER, BOTH SHOULD WORK
    if Request("LastName")= "" then
    Session("badLastName")="T"
    Session("Errors")=Session("Errors") + 1
    end if


    if not IsDate(Request("SendDate")) then
    Session("badDate")="T"
    Session("Errors")=Session("Errors") + 1
    end if


    if Session("Errors") > 0 then
    ‘there were errors, so send back to form
    response.redirect "form.asp"
    else
    'there were no errors, so do the update to the database and redirect to a thank you page
    response.redirect "thanks.asp"
    end if

    %>

    I am using the code above for the time being because in the form.asp I would like to tell the user exactly the field that has not been completed rather than saying the form is not completed.
    But still I am not happy with it as I see many if else statement.

    The other thing I would like to ask is how can I check for the valid UK phonenumbers, postcodes and also valid form of emails?

    One more thing I also have a drop down menu with options that the user should choose one of those. At the moment I haven't done any validations for those. How can I do it any suggestions or do I really have to do it?

    Last thing one the user submitted the form I don't want the data to be reset that the user has already entered, rather than that I would like to keep the data that the user has already entered and give the error message for them to complete the form.

    Thanks very much.

  • #10
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Well what I was saying is you could do something like this.

    In validation.inc: Put all your form validation stuff here.


    On the page with the validation.

    Code:
    <!--#include file="validation.inc"-->
    
    <%
    ...process the form...
    %>
    The method I was suggesting was just to move the code out of the file, so when your working on it, it isn't as cluttered. You'd probably want to do it in combination with the method Roy suggested.
    OracleGuy

  • #11
    Regular Coder
    Join Date
    Jan 2003
    Posts
    148
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a lot for the help.
    Do you have any ideas of doing other validations I have mentioned above?

    Thanks

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I guess I must be doing something right with VBScript then, I haven't run into any of the problems you mentioned, Glenn. Probably because I started with JavaScript?

    Either that or I haven't run into any of the illogical errors you describe ?!?

    Do you have an example?
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #13
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    P.S. pinkcat_02, I don't know what the validation criteria for UK postcodes or phonenumbers are, but if you can provide me with the exact criteria, I will attempt to write a regular expression validation function for them.

    As for email... I use this:

    Function IsEmail(str)
    Dim ieRegEx
    Set ieRegEx = New RegExp
    ieRegEx.Pattern = "^[\w\+\'\.-]+@[\w\'\.-]+\.[a-zA-Z0-9]{2,}$"
    IsEmail = ieRegEx.Test(str)
    End Function

    It seems to work ok for now, but it is somewhat loose and most likely not absolutely technically correct, since it appears there are other characters that "may" appear before the @ sign, however with this regex I haven't run into them, so if that's the case they must be VERY rare.

    If someone were able to translate the appropriate RFC into human-readable English, I could do better, but apparently noone in the world is capable of doing so (and I can't make heads or tails of it!).
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #14
    Regular Coder
    Join Date
    Jan 2003
    Posts
    148
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I didn't put my validation in a function as I am not very sure how to do those so instead I used many if else statements so I have tried to put the function you have advised in a if else statement like:

    if not IsCustomer_Email(Request("Customer_Email"))
    Dim ieRegEx
    Set ieRegEx = New RegExp
    ieRegEx.Pattern = "^[\w\+'\.-]+@[\w'\.-]+\.[a-zA-Z0-9]{2,}$"
    IsEmail = ieRegEx.Test(str)

    then

    Session("badCustomer_Email")="T"
    Session("Errors")=Session("Errors") + 1
    end if

    But it is not working that I am sure it might not be the way I have done it. How am I supposed to do that?

    Thanks

  • #15
    Regular Coder
    Join Date
    Jan 2003
    Posts
    148
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh ignore my stupidness please I have sorted it out. For the UKpostcode and phonenumber maybe I can try them out myself first and get back to you if I get stuck but I just wonder where I can find a tutorial of writing expression like the format you did? I better learn and be able to write it myself

    Thanks


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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