Samuel Sjöberg's weblog

Skip to navigation

vForm and a single checkbox

I have recieved some emails and also comments on the weblog about the fact that a single checkbox cannot be validated with vForm 2.0. The problem lies in the javascript implementation of the required rule.

If a checkbox or a radio button is validated, the following rule is used:

return element.form[element.name].length > 0

Unfortunately, the length will be undefined if the element isn't part of an array. The following statement fixes this problem:

if (typeof element.form[element.name].length == 'undefined') {
    return element.checked;
}
return element.form[element.name].length > 0;

With this fix in place, the validation of single checkboxes and radio buttons should be working like expected. Note that you should use the simple required rule (i.e., the empty rules set: <rules/>).

vForm 2.0.2

The vForm 2.0.2 release contains the following fixes.

Pages linking to this entry

Pingback is enabled on all archived entries. Read more about pingback in the Pingback 1.0 Specification.

About this post

Created 3rd December 2006 17:54 CET. Filed under Javascript and DOM.

0 Comments
0 Pingbacks