logo

New Response

« Return to the main article

You are replying to:

    • avatar
    • TippChick
    • Posted on Thu 14 Mar 2002

    Have scanned through several of the responses etc, and I haven't seen any mention of this, so I thought I'd throw it it to see if you can make use of it. Apologies if I am repeating others...

    I use very similar code for validation, except when I call the Submit, I dont pass in the field names of the fields to be validated.

    I give all required fields a class of Mandatory ( in the HTML Properties ), and in the validation code, I loop through all the elements and only validate the ones where element[i].className = 'Mandatory'.

    Then I use element[i].type to decide which field type I have to validate... ( Oh yes, I also stop it from validating hidden fields, when element[i].type == 'hidden'. )

    The Error message to be returned, can then pick up element[i].title ( which you fill with suitable values )

    Nice thing about this, is you can set up a Style for Mandatory, and set all fields to Yellow ( I prefer moccasin ) to show user that they are mandatory.

    An extension on this is that you can set up other classes, such as Date or MandatoryDate or length1to4 etc, which will validate all your date fields, and so on.

    The radio/checkbox elements need to be tested a different way, becuase otherwise they get validated as many times as there are options in the field. So in the validateRadio function you need to do this to get the group instead of the element

    var radiofield = eval('document.forms[0].'+field.name)

    and then the code is the same as Jakes, and in the calling function, I only validate radio elements, if the element[i].name <> element[i+1].name ( ie its the last element in the radio group ).

    Works for me.

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment: