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.
Results 1 to 6 of 6

Thread: form validation

  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    form validation

    Hi,

    Just to let you know I am very new to Javascript and i need help.

    I am trying to validate a form with lots of fields. I have to check that something has been entered in a large proportion of these fields. Is there a better way to do it than using lots of if statements i.e if (hospitalNo =="").........if(dob=="").........

    K

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    This belongs in the Javascript Programming forum.

    Please try to post in the more appropriate forum next time, but don't worry about it.

    :: moving thread ::

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    http://www.codingforums.com/showthre...?threadid=2510

    Let us know if you need to flag only certain fields as 'required'.

    [isn't jkd polite?]

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am trying to validate a form with lots of fields. I have to check that something has been entered in a large proportion of these fields. Is there a better way to do it than using lots of if statements i.e if (hospitalNo =="").........if(dob=="").........
    It depends on just what you're validating against as to how you do it. Off had I'd say build a somewhat generic function since you are checking so many fields. Pass in two parameters, the value your checking and a list (array) of values you're validating against. This second parameter will be either a list of what's OK or a list of what's not OK - which ever is easier. Here's what the code might look like:
    Code:
    function validateField (thisfield, validValues)  {
       var IsValid = new Boolean (false);
    
       for (var i = 0; i<validValues.length; i++)  {  
          if thisfield == validValues[i].value
             IsValid = true;
       }
       return IsValid;
    }  // function validateFields()
    NOTE: if you're validating against a limited number of values, and all the fields you're checking are validated against the same values, then you can use a switch statement. Personally I like using switch statments whenever I can. I think they "self document" very well and are easy to add or delete cases from w/out touching the basic structure of the code. And ALWAYS include a default case.

    NOTE: I may not have the right properties for array here, but you get the idea.
    NOTE: Using the same basic code structure in the function above, you can use a regular expression instead of an array of values - again, it depends on what you're validating against.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, another way, it checks all the fields at once.... IF all the fields are consecutive on the form then you can loop through the form.items and check against, say blank. Something like this. Again don't hold my feet to the fire on syntax; but this is doable.

    Code:
    function validateForm (thisform) {
       FormIsValid = new Boolean (true);
    
       for (var i=0; i<20; i++) {
          if thisform.items[i].value == ""
             FormIsValid = false;
       }
       return FormIsValid;
    }  // validateForm

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

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