Form validation

Author Message
anna22ben

  • Total Posts : 6
  • Scores: 0
  • Reward points : 0
  • Joined: 6/4/2009
  • Status: offline
Form validation Monday, July 06, 2009 12:09 AM (permalink)
0
Hi all,
I am very new to ASP, and need some help.

I have traced some open-source code for form validation, and need to ammend it.

Firstly, I would like to know how to validate for numbers only (telephone). I know isNumeric comes in somewhere, but i don't know where to place it!
Here is the code in question so far:

' telephone field to be no less than 11 characters
 IF len(Form_Telephone)<11 THEN
 Validated_Form = false
 END IF

' telephone field to be no greater than 12 characters
 IF len(Form_Telephone)>13 THEN
 Validated_Form = false
 END IF

As said, I would also like to check for numbers only..

Secondly, the error message on this form is pretty useless - it simply says: "errors detected, go back and fix them". It does not say on what fields the errors are and why!

I would like to know how to ammend that.
Say, for example, an email field. I would like a specific error message to read: "Please enter a valid email address".
This would need to be used for all the fields, ie: name, address etc.

Look forward to help
 
#1
    ebgreen

    • Total Posts : 8227
    • Scores: 98
    • Reward points : 0
    • Joined: 7/12/2005
    • Status: offline
    RE: Form validation Monday, July 06, 2009 2:57 AM (permalink)
    0
    Well, I am not an ASP guy, so take all my comments with a large grain of salt. For the validation part, I can think of two ways to approach it. If you only allow numbers in the field, then after you check the length (which by the way you could do in one If-Then instead of two if you wanted) then you could do this:
     
    If Not IsNumeric(Form_Telephone) Then
        Validate_Form = False
    End If
     
    The second option would be to validate via a regular expression. We can talk about how to go that route if the IsNumeric does not work, but I would consider it to be overkill in this case.
     
     
    As for the error display, somewhere in your code you are going to find a spot where the value of Validate_Form is checked and right below that will be the line that pops the error message. Here is how I would implement what you want. I would make a new variable (let's call it Form_Error) and give it a value that is the same as the error message that you get right now. Then search your code and find all the places where Validate_Form = Flase appears. At each of these places, put a line below that that will set Form_Error to whatever message you would want to display for that particular error condition. Then go back to the part of the code that checks the value of Validate_Form. Instead of popping the current message, have it pop whatever the contents of Form_Error is.
     
    Note that I would consider this to be an imperfect solution since by it's nature it will only display the message for the last error discovered. If this is acceptable, then enjoy. If instead you would prefer to have it list all the validation errors, we can discuss a way to do that as well.
    "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
    Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
    http://www.visualbasicscript.com/m_47117/tm.htm
     
    #2
      anna22ben

      • Total Posts : 6
      • Scores: 0
      • Reward points : 0
      • Joined: 6/4/2009
      • Status: offline
      RE: Form validation Monday, July 06, 2009 7:56 AM (permalink)
      0
      Thankyou for the reply.
      I've tried the  If Not IsNumeric, but it doesn't seem to work..
      It doesn;t throw up an error, but still allows anything to be entered.

      As for the errors, I was hoping to be able to provide a list of all errors.

      Hope you can help some more..




       
      #3
        ebgreen

        • Total Posts : 8227
        • Scores: 98
        • Reward points : 0
        • Joined: 7/12/2005
        • Status: offline
        RE: Form validation Monday, July 06, 2009 8:05 AM (permalink)
        0
        For the Not IsNumeric, are you using On Error Resume Next in there somewhere? If so, comment it out and see if you get an error.
         
        Now, for the error reporting here is how I would do it to get a list. Do just what I said before except:
         
        When you first intialize Form_Error make it be Form_Error = "Form validation errors were detected:"
        Then in each location where you have a Validate_Form = False add a line like this:
         
        Form_Error = Form_Error & vbCrLf & "The phone number has non numeric characters"
         
        Except you would change the part in quotes to be appropriate for the error being tested at that point of course.
        "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
        Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
        http://www.visualbasicscript.com/m_47117/tm.htm
         
        #4

          Online Bookmarks Sharing: Share/Bookmark

          Jump to:

          Current active users

          There are 0 members and 1 guests.

          Icon Legend and Permission

          • New Messages
          • No New Messages
          • Hot Topic w/ New Messages
          • Hot Topic w/o New Messages
          • Locked w/ New Messages
          • Locked w/o New Messages
          • Read Message
          • Post New Thread
          • Reply to message
          • Post New Poll
          • Submit Vote
          • Post reward post
          • Delete my own posts
          • Delete my own threads
          • Rate post

          2000-2012 ASPPlayground.NET Forum Version 3.9