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 14 of 14
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts

    multiple field elements check

    I get N fields (N is unknown) named as follow:
    fieldname1
    fieldname2
    ...
    fieldnameN

    --> It's generated from server side...

    And I want to run a validity check on those fields before I submit.
    The problem is that I can't tell how many fields I have, and even if so, I don't know how to query about each field, since their names are changing from one to another.

    Can anyone help please?
    Thanks

    PS
    Ofcourse I can make my server side script, write the JS script with check for each field. But I'm looking for the client side function.
    Last edited by ShMiL; 10-10-2003 at 02:33 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Each form object has an "elements" array associated with it which you can use to scan through the whole form and find those fields regardless of how many actually exist on the form.

    document.forms["formname"].elements[x] -- where x is a number or a literal field name.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can get the number of elements in a form by doing this:
    var frmlen=document.forms[0].length

    "forms[0]" would be the first form in your document. You could then use this variable to loop through the form elements like this:
    for(i=0;i<frmlen;i++)
    {
    if(document.forms[0].elements[i].type="text")
    {do some validating}
    if(document.forms[0].elements[i].type="checkbox")
    {do some validating}
    etc ......
    }

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thank you both
    but those are not the ONLY fields in the form, so I can't know what number of element to start from, and where to stop the checking...

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Requestcode already showed you the code for iterating through the elements array. The elements array contains object references to each form element. That means you can check all kinds of information about each form element including the name of that element. With the name you can simply check to see if the name matches the pattern you're expecting and then perform whatever validation you want on fields which match.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    great!
    Thanks

    Another question if I already posted...
    When I put DISABLED in a form field, it becomes un-accessable and is also ignored when I submit the form.

    Is there a way to only make it not accessable (yet visible) and not ignoring it upon submission?

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, replace DISABLED with READONLY.

    http://www.w3.org/TR/html4/interact/forms.html#h-17.12
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    that's it.
    Is there something parallel to READONLY for checkboxes and radio buttons?

    Thanks

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If READONLY does not work then try this: onFocus="this.blur()" if you don't want someone to change them.

  • #10
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    it's quite the same
    since blur, comes after click
    and it's possible to change checkboxes and radios....

    Any other idea?

  • #11
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This disables a radio button:
    <input type="radio" name="r1" value="Radio1" disabled>

    If you want to disable the above using javascript then it would be:
    document.forms[0].r1.disabled=true
    and to enable it:
    document.forms[0].r1.disabled=false

  • #12
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    read above!
    When I use DISABLED, the radio is ignored on submission!
    I don't want to ignore it, just don't want the user to change it!

    But I have an idea!!!
    Is there a way to reset (<input type="reset">) only part of the form?

  • #13
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Since you already have the code for iterating through the form why not use it AFTER you've made all the other checks on the form to check for form elements that are checkboxes or radio buttons and are disabled and change them from disabled to enabled.

    Of course since you may have customers with Javascript enabled you're going to have to handle the case where the elements don't come back anyway so I'd just skip the whole process since your server side code is going to have to cope with elements not returning anyway. (Not to mention the fact that you should never accept and update data if you send it to a user as read-only).
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #14
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”


  •  

    Posting Permissions

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