Jel. FormValidatorProvides unobtrusive client-side validation behaviour to an HTML form, via recognition of special CSS class strings (validation classes) attached to each field. Includes customisation options for how to display the validation errors (results), how to change the display of “culprit” fields and their labels (through css classes) and also the option to display these errors adjacent to each field in “inline” containers (on field blur). Error messages are automatically generated through use of internationalizable error template strings, but can also be entirely customized if the automatic messages are insufficient. See below for terminology used within this documentation. Terminology| culprit | A culprit field is a field that has been deemed not valid when validating the form it belongs to. | | results | The results of the form validation refers to the information gathered about the culprits during that validation (if any, that is, if validation fails). Displaying the results usually refers to displaying the error messages that describe what is wrong with each culprit field to the person filling in the form. | | validation class | the special CSS classes that are applied to form elements which describe rules for them to be valid. Many pretermined classes are available to use, or you can define your own custom validation classes (functions) and associated error templates (see below). | | error template | a string template resource linked to each validation class that defines the error message to show when that validation rule fails. For custom validation classes, an equivalent error template needs to be defined also. | | error message | the message for each culprit field describing why validation has failed. (this is the “filled-in” version of the error template for the validation class rule that has not been satisifed). | | inline validation | if activated, individual fields will be validated on blur. If this validation fails, an inline error message (a variation of the standard error message, without the field label) will be placed inside the associated inline (see below) | | inline | an optional container element defined for each field (typically adjacent to it in the DOM) that will be filled with any inline error messages generated during inline validation. |
Validation ClassesThe validation classes recognised by Jel.FormValidator are specified in the class attribute of each field in the form being validated. The range of supported classes is quite extensive and automate validation of many conditions that you previously would have written custom code for. Not only that, but all of the validation classes are able to build readable error messages that describe what went wrong to the user. They can be be placed into 4 major categories: | required | the special case of a field simply being required | | length based | checks the character length of a field against a specified value, or lower and upper values | | value based | checks the value of a field against a specified comparison value, or upper and lower value, or even another field value | | pattern based | special cases such as email addresses |
It’s also important to note that most of these validation classes are derived from the function names in the Jel.Validator class, and they call out to those functions to check fields. The “required” validation classTo mark a form field as being required (that is, a value must be provided to be able to submit the form), simply add the class “required” to the class attribute of the field. Length-based validation classesLength based classes check that a field in the form matches a certain character length condition. These follow the format length-OPERATOR-COMPARE where: | OPERATOR | is the comparison operator being performed (see below) | | COMPARE | the comparison length value, or lower and upper value if OPERATOR is “range” |
Values for OPERATOR| eq | equality comparison (==) | | neq | inequality comparison (!=) | | lt | less than (<) | | gt | greater than (>) | | le | less than or equal to (<=) | | ge | greater than or equal to (>=) | | range | between lower and upper bound (in a...b) |
Values for COMPARE| a | where a is an integer, if OPERATOR is not “range” | | a:b | where a and b are integers representing lower and upper bounds if OPERATOR is range, and you want the bounds to be non-inclusive | | a::b | as above if OPERATOR is range, and you want the bounds to be inclusive |
Examples of length-based validation classeslength-gt-5 - field length must be greater than (gt) 5 characters length-eq-5 - field length must be equal to (eq) 5 characters length-range-5:10 - field length must be between 5 and 10 characters, non-inclusive (:) length-range-5::10 - field length must be between 5 and 10 characters, inclusive (::)
Value-based classesValue-based classes check a field’s data type and value compared to another value, or lower and upper values if the operator is a range. These values can either be constant, or can specified as the values of other fields in the form. These follow the format [TYPE-]OPERATOR-COMPARE-FORMAT where: | TYPE | is the data type that is expected in this field. | | OPERATOR | is the comparison operator being performed. | | COMPARE | is the value, upper and lower value, or even another field to be compared | | FORMAT | is the date format when TYPE is “date” or “time” |
Refer to the reference below for possible values of each of these attributes. Values for TYPE| empty (not specified) | for strings, that is, no type check really occurs at all | | int | for integers | | float | for floating point numbers, that is, they have decimals | | numeric | for int or float | | date | for date strings. When specifying a date, the FORMAT must also be specified | | time | synonym for “date”. This is more readable when you’re using a time format |
Values for OPERATORThe same as for length-based classes, with the extra exceptions: | (int|float|numeric)-positive | a positive number | | (int|float|numeric)-negative | a negative number | | date-future-FORMAT | a date string in the future in FORMAT | | date-past-FORMAT | a date in the past in FORMAT | | time-earlier-FORMAT | a time earlier than the current time time-later-FORMAT- a time later than the current time |
Values for COMPARE (comparing constants)The syntax for COMPARE follows the same rules as for length-based comparisons, but for different types, that is: | a | where a is an appropriate type (see below), for a single value comparison | | a:b | where a, b are an appropriate type (see below), for “range” lower and upper bounds (non-inclusive) | | a::b | where a, b are an appropriate type (see below), for “range” lower and upper bounds (inclusive) |
Types are represented like so| TYPE is empty (string) | whatever string you want to compare | | TYPE is int | an integer | | TYPE is float | an integer, followed by lowercase “p” (for point), then another integer for decimal part. E.g. 3p14159 | | TYPE is date | a representation of a date in the format yyyymmdd (e.g. 20070403) for just a date, and yyyymmddThhmmss for date AND time. (e.g. 20070403T133000) | | TYPE is time | a representation of a time in the format hhmmss (e.g. 133000) |
Values for COMPARE (when comparing other fields)If you want to compare the value of the field with another one in the same form, simply substitute the constant in the class string with the ID of the field(s) to compare, followed by the suffix in the “suffixCompareField” in Options hash of the constructor (by default this is “-field”). Values for FORMAT (only appropriate for “date” and “time” types)a dash-delimited version of a property identifier for the formatting constants in Jel.Date.FORMAT. E.g. date-future-uk (Jel.Date.FORMAT.UK), date-future-uk-12 (Jel.Date.FORMAT.UK_12) Examples of value-based validation classesint-ge-4 - integer greater than or equal to 4 int-range-4:8 - integer between 4 and 8, non-inclusive float-range-4p5:20p1 - floating point, between 4.5 and 20.1 neq-password - not equal to "password" case-sensitive eq-password-field - equal to the value of the field with ID "password" (useful for confirm password) gt-ci-a - greater than "a", case-insensitive lt-b - less than "b", case-sensitive date-future-uk - UK formatted date (dd/mm/yyyy) in the future date-ge-20000101-us - US formatted date (mm/dd/yyyy) sometime after the turn of the millenium (1st Jan 2000) date-range-uk-20050201::20070301 - UK formatted date between 1st Feb 2005 and 1st Mar 2007, inclusive
Hopefully it’s clear from the rules above that they are a lot easier to construct than they are to explain! Summary | Provides unobtrusive client-side validation behaviour to an HTML form, via recognition of special CSS class strings (validation classes) attached to each field. | | | | The HTML form associated with this validator. | | A hash of field labels (the text, not the label elements themselves) indexed by the ID of the related control. | | | | | | Registers a custom error message and custom inline error message for a given fieldId, and className (validation class). | | Prevents the specified fields from being regarded in form validation. | | Causes the specified fields to be regarded in form validation once again. | | Prevents the fields with the specified name from being regarded in form validation. | | Causes fields with the specified name to be regarded in form validation once again. | | Registers the specified fields to be validated by the form validator. | | Removes the specified fields from the form validator. | | displays a given field as a validation culprit. | | displays a given field as being valid, that is, NOT a validation culprit. | | displays an inline element for a validation culprit. | | hides an inline element for a field that is likely no longer a validation culprit. | | checks if a given field is currently classed as a culprit (via CSS) | | Marks the given field as a culprit field in the validation results. | | Attempts to submit the form associated with this validator, by running all validation, and submitting if successful. | | Validates the form associated with this validator, and sets up validation results. | | Displays all of the current culprit fields for the latest validation. | | Shows all of the form fields as non-culprit. | | Displays the results of the validation, based on options specified in the constructor method. | | Hides the results of the validation, and also any inlines associated with previous validations (if visible). | | The method called to display the validation results as a JavaScript alert, if the options specify this (see constructor for more). | | The method called to display the validation results inside a DOM container (controlled via the resultsContainer option in constructor). | | The method called to hide the DOM container that validation results are displayed in (controlled via the resultsContainer option in constructor). | | The default method for formatting a field label when displaying them in validation error messages or inline error messages. | | The default method for formatting date format strings when displaying them in validation error messages or inline error messages. | | Validates a single given field in the form associated with this validator, and sets up the results for just this field. | | Checks if a given field is valid within the form associated with this validator. | | Checks if a given field is “empty” within the form. For textareas and text inputs, this is when the field value is the empty string. | | Gets the inline error message container for a given field. |
formThe HTML form associated with this validator.
fieldLabelsA hash of field labels (the text, not the label elements themselves) indexed by the ID of the related control.
constructor| constructor: function( | form, | | options | ) |
|
Class constructor. Parameters| form | HTML Form Element, the form to apply form validation behaviour to | | options | hash, a hash of options to configure the validator (see below) |
Options available (specified in options hash)| callbacks | hash, a collection of optional callbacks to customise the behaviour of the form validator (see below) | | validators | Hash, a collection of custom validators to use for this form, indexed by the camelized class names that they match (for example, a “user-name” class would be indexed “userName” in this hash) | | errorTemplates | Hash, a collection of errorTemplates to use for any custom validators in validators (each validator requires an error template), indexed by the uppercase-underscore-delimited version of the class name (for example, a “user-name” class would be indexed “USER_NAME” in this hash) | | culpritFieldClassName | String (default “culprit”), the class name to apply to fields that are responsible for the form not validating (termed ‘culprits’) | | culpritLabelClassName | String (default “culprit”), the class name to apply to the labels of culprit fields | | selectEmptyValue | String (default “”), the value of a select field that is regarded as empty (that is, not yet chosen) | | validateInline | Boolean (default true), indicates whether single field validation should occur on each field’s onblur event, and be displayed in associated “inline” elements | | suffixCompareField | String (default “-field”), the suffix used to describe field comparisons in validation classes | | suffixInline | String (default “-inline”), the suffix added to a field ID to determine the ID of an “inline” container, used to display inline error messages for that field | | resultsContainer | HTML Element, if specified, the form validator will build an XHTML error message inside this container, which consists of an intro paragraph, followed by an unordered list of all error messages) | | resultsAlert | Boolean, if true the form validator will display a nicely formatted JavaScript alert box showing your errors. This will also occur by default automatically if no other display has been specified, that is if both options.callbacks.onShowResults and options.resultsContainer are not specified. |
Callbacks available (specified in options.callbacks hash)| formatFieldLabel(label, field) | If specified, the string this function returns will be used as the field label when it is shown in error messages | | formatDateFormat(format) | If specified, the string this function returns will be used as a date format when they are shown in error messages | | onShowResults(results, validator) | If specified, will be called when displaying the results of a validation (only occurs is there are problems). Refer to showResultsAlert for information on the results parameter. | | onValidate(validator) | If specified, this will be called after regular field validation has occurred, to allow you to perform any custom validation (return false if not the form is still not valid, true otherwise) | | onSubmit(validator) | If specified, this function will be called once all validation is successful, but before the form is submitted. Return false to prevent form submission, and true to let it through. Preventing form submission (returning false) could be an approach for AJAX applications that hijack a standard form submit. |
Examplevar validator = new Jel.FormValidator ( $('contact-form'), { callbacks: { formatFieldLabel: function(label) { return '<em>' + label.replace(/[:\*]/g, '') + '</em>'; },
onSubmit: function(validator) { this.sendAjaxRequest(Form.serialize(validator.form)); return false; } } } );
registerErrorMessage| registerErrorMessage: function( | fieldId, | | className, | | errorMessage, | | errorInlineMessage | ) |
|
Registers a custom error message and custom inline error message for a given fieldId, and className (validation class). Note that this is not an error template, rather, it is the final error message that will be displayed. Parameters| fieldId | String, the ID of the field | | className | String, the validation class to attach error messages to | | errorMessage | String, the error message to display when validation on the field fails for the validation rule described by className. | | errorMessage | String, the inline error message to display when validation fails. |
Examplevalidator.registerErrorMessage( $('username'), "length-ge-6", "Username must be at least 6 characters long", "must be at least 6 characters long" );
disableFields| disableFields: function() |
Prevents the specified fields from being regarded in form validation. This would be essential if you have elements that are hidden or disabled based on the values of other elements, since you wouldn’t want to display error messages for fields the user can’t see or edit. Parameters| fieldId (variable) | String(s), A variable amount of element IDs for fields you want disregarded in validation, or the element “name” attribute for radio buttons and checkboxes |
Examplevalidator.disableFields("billing-address", "billing-state", "billing-post-code", "billing-city", "billing-country"); // suppose you had a checkbox labelled "use shipping address" which disabled billing address fields // which you wanted to disregard in validation
See also: enableFields
enableFieldsCauses the specified fields to be regarded in form validation once again. This would be generally called for fields tat had been previously disabled by disableFields. IMPORTANT: Don’t call this method if you are adding new fields to the form and want them regarded in validation. Please use addFields instead for this purpose. Parameters| fieldId (variable) | String(s), A variable amount of element IDs for fields you want disregarded in validation, or the element “name” attribute for radio buttons and checkboxes |
Examplevalidator.disableFields("billing-address", "billing-state", "billing-post-code", "billing-city", "billing-country"); // suppose you had a checkbox labelled "use shipping address" which disabled billing address fields // which you wanted to disregard in validation
See also: disableFields
disableFieldByName| disableFieldByName: function( | name | ) |
|
Prevents the fields with the specified name from being regarded in form validation. This should only be used for radio buttons and checkboxes. Parameters| name | String, the “name” attribute for radio buttons and checkboxes you want disabled (that is, to be ignored in validation). |
Examplevalidator.disableFieldByName("interests");
See also: disableFields, enableFieldByName
enableFieldByName| enableFieldByName: function( | name | ) |
|
Causes fields with the specified name to be regarded in form validation once again. This should only be used for radio buttons and checkboxes. Parameters| name | String, the “name” attribute for radio buttons and checkboxes you want enabled. |
Examplevalidator.disableFieldByName("interests");
See also: disableFieldByName, enableFields
addFieldsRegisters the specified fields to be validated by the form validator. This call is ESSENTIAL for fields that are added as the user interacts with the page, and it should be called after fields have been added inside the form tag for this validator. See below for why this is not handled automatically. Parameters| element (variable) | Element(s), a variable number of fields to add to the validator |
Reason for this methodFor performance reasons, the form validator analyses the fields inside its associated form only when the class is constructed, where it builds quickly accessible information it can use for subsequent validations. It does not check for new fields in the form each time the form is validated, so you need to call this method if any new fields are added to the form. Examplenew Insertion.Top($("extra-credentials"), "<label for="alternate-username">Alternate Username:</label>" + "<input id="alternate-username" anme="alternate-username" class="length-ge-7" />" + "<div id="alternate-username-inline" class="inline"></div>");
validator.addFields($("alternate-username"));
See also: removeFields
removeFieldsRemoves the specified fields from the form validator. This call is ESSENTIAL for fields that are removed as the user interacts with the page, and it should be called before fields have been removed from the form. See addFields for a discussion of why this is not handled automatically. Parameters| element (variable) | Element(s), a variable number of fields to remove from the validator |
Examplevalidator.removeFields($('alternate-username')); $("extra-credentials").innerHTML = ""; // assuming "extra-credentials" had "alternate-username" control inside it.
displayCulprit| displayCulprit: function( | field, | | isInline | ) |
|
displays a given field as a validation culprit. While this method is best utilised internally by this class, it can be overridden to provide more sophisticated behaviour if so desired. Parameters| field | Element, the field to display as a culprit | | isInline | when called by this class, indicates whether this culprit display is occurring for inline validation (on field blur) |
Examplevalidator.displayCulprit($('username'), true);
See also: releaseCulprit, displayInline
releaseCulprit| releaseCulprit: function( | field | ) |
|
displays a given field as being valid, that is, NOT a validation culprit. While this method is best utilised internally by this class, it can be overridden to provide more sophisticated behaviour if so desired. Parameters| field | Element, the field to display as a non-culprit | | isInline | when called by this class, indicates whether this non-culprit display is occurring for inline validation (on field blur) |
Examplevalidator.releaseCulprit($('username'), true);
See also: displayCulprit, releaseInline
displayInline| displayInline: function( | inline, | | message | ) |
|
displays an inline element for a validation culprit. While this method is usually best utilised internally by this class, it can also be overridden to provide more sophisticated behaviour if so desired. Parameters| inline | Element, the inline element to display, for a culprit field | | message | String, the inline error message being displayed |
Examplevalidator.displayInline($('username-inline'), "username already in use");
See also: displayCulprit, releaseInline
releaseInline| releaseInline: function( | inline | ) |
|
hides an inline element for a field that is likely no longer a validation culprit. While this method is usually best utilised internally by this class, it can also be overridden to provide more sophisticated behaviour if so desired. Parameters| inline | Element, the inline element to hide, for a non-culprit field |
Examplevalidator.releaseInline($('username-inline'));
See also: displayInline, releaseCulprit
classedCulprit| classedCulprit: function( | field | ) |
|
checks if a given field is currently classed as a culprit (via CSS) Parameters| field | Element, the field to check |
addCulprit| addCulprit: function( | field, | | errorMessage, | | errorInlineMessage | ) |
|
Marks the given field as a culprit field in the validation results. This would be most useful in the onValidate callback (see options for constructor) to add a field as a culprit before results are displayed. This would generally be based on it failing some complex condition that can’t be described simply by validation classes on the field. Note that this does not display the fields as a culprit, it just registers it in the validation results. Parameters| field | Element, the field to register as a culprit | | errorMessage | String, the standard error message to associate with the culprit field in the results | | errorInlineMessage | String, the inline error message to associate with the culprit field in the results |
Exampleif (parseInt($('percentage-male') + parseInt($('percentage-female'))) != 100) validator.addCulprit($('percentage-male'), "Percentage Male and Female must total to 100 percent", "Must total to 100 percent");
submitAttempts to submit the form associated with this validator, by running all validation, and submitting if successful. Note that you would only need to use this if you have not provided a proper submit button, or input type=”image” button, which is not recommended, since it may be inaccessible. If you have provided a submit or image, the validation will be automatically occur during the onsubmit event for the form. Examplevalidator.submit();
validateValidates the form associated with this validator, and sets up validation results. Returns a boolean indicating if the validation as successful or not. Generally, you won’t need to call this method directly.
displayCulprits| displayCulprits: function() |
Displays all of the current culprit fields for the latest validation. This is called automatically after validation occurs if any culprits are present.
releaseCulprits| releaseCulprits: function() |
Shows all of the form fields as non-culprit. This is called automatically before validation to clear any previous validation state.
showResults| showResults: function( | results | ) |
|
Displays the results of the validation, based on options specified in the constructor method. This is called automatically on form submit. Parameters| results | Hash, a hash containing a number of properties that describe the results of validation. See below for more details. |
Results Hash| culprits | Array, an array of the culprit fields, that is those that have caused the validation to fail | | errors | Array, an array of error messages. This is useful if you want to “split” the error messages without needing to know what fields they belong to | | errorsById | Hash, an object hash of error messages, indexed by their corresponding culprit field ID. | | inlines | Array, an array of the inline error messages. The inline messages are generally the same as the standard error messages, but without reference to the field itself, since they are intended to be displayed adjacent to the field. | | inlinesById | Hash, an object hash of the inline error messages, indexed by their corresponding culprit field ID. |
See also: hideResults, showResultsAlert, showResultsList
hideResults| hideResults: function( | results | ) |
|
Hides the results of the validation, and also any inlines associated with previous validations (if visible). This is called automatically just before form submit if the form is valid. Parameters| results | Hash, a hash containing a number of properties that describe the results of validation. See the reference under showResults for details. |
See also: showResults, hideResultsList
showResultsAlert| showResultsAlert: function( | results | ) |
|
The method called to display the validation results as a JavaScript alert, if the options specify this (see constructor for more). This could be overridden to provide more specific functionality. Parameters| results | Hash, a hash containing a number of properties that describe the results of validation. See the reference under showResults for details. |
See also: showResults, showResultsList
showResultsList| showResultsList: function( | results, | | container | ) |
|
The method called to display the validation results inside a DOM container (controlled via the resultsContainer option in constructor). The default behaviour is to display an intro paragraph, which is in the language string Jel.Lang.FormValidator.ERRORS_TITLE, followed by an unordered list. This could be overridden to provide more specific functionality. Parameters| results | Hash, a hash containing a number of properties that describe the results of validation. See the reference under showResults for details. | | container | Element, the container to show results in |
See also: showResults, showResultsAlert, hideResultsList
hideResultsList| hideResultsList: function( | results, | | container | ) |
|
The method called to hide the DOM container that validation results are displayed in (controlled via the resultsContainer option in constructor). Parameters| results | Hash, a hash containing a number of properties that describe the results of validation. See the reference under showResults for details. | | container | Element, the container to hide |
See also: showResults, showResultsAlert, showResultsList
formatFieldLabel| formatFieldLabel: function( | text, | | field | ) |
|
The default method for formatting a field label when displaying them in validation error messages or inline error messages. This can be overridden by specifying the formatFieldLabel callback in constructor options, or you may wish to override this default method for a custom class derived from FormValidator. Parameters| text | String, the text for the field label (usually the innerHTML of its associated <label> element) | | field | Element, the field being displayed |
formatDateFormat| formatDateFormat: function( | format | ) |
|
The default method for formatting date format strings when displaying them in validation error messages or inline error messages. This can be overridden by specifying the formatDateFormat callback in constructor options, or you may wish to override this default method for a custom class derived from FormValidator. Parameters| format | String, the date format string. These are usually one of the properties in the Jel.Date.HUMAN_FORMAT hash, that is, a date format string as understood by humans. |
validateField| validateField: function( | field | ) |
|
Validates a single given field in the form associated with this validator, and sets up the results for just this field. This is called by the on blur event for the field if inline validation is setup Parameters| field | Element, the field to validate |
Returns| true | if the field is valid | | false | otherwise |
Examplevalidator.validateField($('username'));
checkField| checkField: function( | field, | | compareOnly | ) |
|
Checks if a given field is valid within the form associated with this validator. This is called for each field in the form when attempting to submit. Parameters| field | Element, the field to validate | | compareOnly | Boolean, whether the check is simply for comparison which doesn’t generate errors, and is mainly used for comparing two fields with each other. |
Returns| true | if the field is valid | | false | otherwise |
Examplevalidator.checkField($('username'));
isFieldEmpty| isFieldEmpty: function( | field | ) |
|
Checks if a given field is “empty” within the form. For textareas and text inputs, this is when the field value is the empty string. For checkboxes and radios, a field is empty if all of the fields with the same name are unchecked. A select is empty if its value is equal to options.selectEmptyValue (see constructor). This is generally the first check done when a field has the validation class “required”. Parameters| field | Element, the field to check for emptiness |
getInline| getInline: function( | field | ) |
|
Gets the inline error message container for a given field. Parameters| field | Element, the field to get an inline container for |
|