Main Features
- Text fields with the same name are compared and must be identical.
- Text fields of type integer, hex, and email can be defined.
- Text fields can be assigned a minimum length.
- Where appropriate, the features above are implemented for textarea and password fields.
- In addition to built-in validation and highlighting methods, input types that require special handling can be accomodated by defining custom event handlers.
- The following input types are recognised.
checkbox, file, password, radio, select-one, select-multiple, text, textarea
checkbox
Validation fails if the box is not checked.Typically, the text should read I agree to the terms above.
password
Validation fails if the number of characters entered is less than the minimum.The default minimum is 6.
If two password fields have the same name, they must be identical.
radio
Validation fails if no button in the group is checked.select-one
Validation fails if the selected index is zero or the selected item is blank or the selected item begins and ends with a hyphen ('-' character).select-multiple
Validation fails if no items are selected.text
Text fields can be treated as integers or email fields. See below.If two text fields have the same name, they must be identical.
textarea
Validation fails if the number of characters entered is less than the minimum.The default minimum is 1.
Calling the Validation Script
- In the examples below, validation is performed in response to an onclick event.
This method is used because we just want to demonstrate/test the validator.
Normally, validation of a form is done by attaching an event handler to the form's onsubmit event.<form onsubmit="return ufm_validate(this)">
- Generally, you should only need to use the function ufm_validate(form).
This requires at least one parameter, being the form that is to be validated. - If no further parameters are supplied, all controls on the form are validated.
- If further parameter are supplied, they must identify the controls by name (not id).
- The following prefixes may be used to identify text controls that require special handling:-
- : any decimal integer, positive or negative. + : any positive decimal integer. # : any hexadecimal integer. @ : an email address. [n] : minimum length n chars - applies to text, textarea and password fields. - All prefixes, except the minimum length, may be followed by ? to indicate
the field is optional.
Optional fields are not validated if they are blank. - If the first parameter following the form is '!' all fields on the form are validated
except those that are named in the the following parameter list. However, fields specified with
a prefix are always validated.
- Refer to the comments within the script for information on how to use other functions.
Validating Custom Controls
If you require a validation or highlight method that is not provided by the standard script, it can be added very easily. You simply need to create the method and assign it to the control.<input type="text" name="custom" id="custom"> <script type="text/javascipt"> <!-- function validateCustom(pfc) // pfc is the prefix code : see function ufm_getPrefixCode. { return (this.value.toLowerCase() == 'ok') } function highlightCustom(hl) // If hl is true, highlight must be set, if false it must be cleared. { if (hl) this.style.color = '#0000FF'; else this.style.color = '' } // Locate the custom control and assign the handlers. // Note that the event names ‘ufm_on...’ are entirely lower case. var c = document.getElementById('custom'); if (c) { c.ufm_onvalidate = validateCustom; c.ufm_onhighlight = highlightCustom; } // --> </script>