This is part of a series of cross-platform experiments in incremental extensions to HTML4 forms that provide a stepping stone towards the much richer capabilities in XForms. This particular experiment introduces a needed attribute whose value is an expression that indicates whether a value for the field is needed before the form can be submitted.
Please view the XForms-Tiny script for more details on how this works.
Here is the markup for the above form:
<form name="form1"> <fieldset> <legend>Conditionally required field</legend> <label for="f1" title="optional">X</label> <input id="f1" name="x" type="text"/> <label for="f2" title="required if x is filled out">Y</label> <input id="f2" name="y" type="text" needed="x != ''"/> <button type="submit">Submit</button> <button type="reset">Reset</button> </fieldset>
The required attribute can be used to provide an expression that if it evaluates to true requires the field to have a value. The expression can act over other form fields, for instance, when this field is required to be filled out only if some other field has a particular value. It can be used together with the various validation attributes. If you want to indicate that a field is always required you should use required="true".
Currently, the library will refuse to submit a form if any of its fields can be shown to be invalid, including any required fields. It is an open issue as whether this is the correct behavior.
My aim was to support required as the name for this attribute, but I discovered that Opera 9 always returns "required" as the value for this attribute, regardless of what was on the web page. On other browsers, you can still use "required as the name for this attribute.
Dave Raggett <dsr@w3.org>Copyright © 2006 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark, document use and software licensing rules apply.