Yesterday, I provided a simple tool to demonstrate the differences between keydown
and keypress
events. Today, I'm going to apply that knowledge toward making a lightweight form validation library. This library will prevent invalid characters from being entered in form input elements which is useful toward preventing malicious data from being entered (think XSS), as well as ensuring data integrity (data is in the valid format, all required fields present, etc.).
Sample Form:
* denotes required field.
The form above should only allow valid characters in each input field - this is your keystroke validation. Furthermore, for fields that require a specific format, when you leave a field with invalid formatted data it should provide an immediate visual indication of the error by setting a
fieldWithErrors
CSS class on invalid fields. Additionally, a fv:onInvalid
event is fired on the invalid element to allow custom events handlers to provide additional actions. For example, enter a number in the date field and tab out. The field will be outlined in red from the fieldWithErrors
CSS class and the message, "Date must be yyyy-mm-dd" will be shown by the handling of the fv:onInvalid
event. Note: the CSS class will be removed automatically when the field is corrected, and a corresponding fv:onValid
event will be fired to allow the custom code to clean up after itself.Validation rules are defined as Regular Expressions. If you are not familiar with Regular Expressions here is a good reference. Additionally, many predefined regex rules for various validations can be found at the regex library.
Several common data types and formats are defined by default in the library, but it also provide the mechanism to add you own data types. In the custom entry, the
type
entry allows you to specify an on-the-fly keystroke validation regular expression, and the format
entry allows you to specify an on-the-fly field format validation regular expression.The Validate Form button demonstrates the action you would want to perform before a form submission. It invokes a
validateAllFields
function which just runs all the field validations (and invokes corresponding CSS rules and issues events). It also returns an array of the invalid field names in case you want to build a list of errors (like the Rails flash messages).Usage Instructions
Sample Usage:
<script src="javascript/formValidation.js"></script>
<script>
var vf = new FormValidation();
vf.addValidationForField("date", "date", "date");
</script>
addValidationForField
requires 3 parameters:- The identifer of the field to add validation for
- The name of the data type to use for keystroke validation
- The name of the data format to use for field validation
In this sample all the names are the same, but they usually wouldn't be.
Just doing the above will prevent invalid characters from being entered in the data field and can indicate when the field is invalid.
When a field is changed, the format validation is run and will do two things.
- It will mark invalid fields are the
fieldWithErrors
CSS class. This can be used to appropriately highlight the error to the user. For example, the following CSS rule will outline the field in red:
.fieldWithErrors {
border: 3px solid red;
} - An event will also be issued in the field to indicate whether or not the field is valid. The events to watch for are
fv:onInvalid
andfv:onValid
. These events can be used to hide/show error messages such the following which will display a pre-defined error message:
$$('input').each(function(inputElem) {
inputElem.observe("fv:onInvalid", function() {
$(this.identify() + "-error").show();}.bind(inputElem));
inputElem.observe("fv:onValid", function() {
$(this.identify() + "-error").hide();}.bind(inputElem));
});
Or any number of other actions. For example, you may wish to display a popup error message such as those provided by Prototype Window or add messages to an error flash area as commonly done in Rails apps or toggle the submit button as enabled/disabled. Using these events allows you to add whatever actions you desire when the
validation fails (or passes).
Download
formValidation.js