Recommend this page to a friend! |
Classes of Jorge Castro | Gentelella BladeOne | vendors/validator/README.md | Download |
|
DownloadvalidatorThe javascript validation code is based on jQuery. The Validator is cross-browser and will give you the power to use future-proof input types such as ?tel?, ?email?, ?number?, ?date?, and ?url?. I can sum this as a ?template? for creating web forms. In the semantic point-of-view, I believe that this method is very clean and?appropriate. This is how forms should be, IMHO. Why should you use this?
Validation types supportHTML5 offers a wide selection of input types. I saw no need to support them all, for example, a checkbox should not be validated as ?required? because why wouldn?t it be checked in the first place when the form is rendered? For a full list of all the available Types, visit the working draft page.
These input types can be validated by the the JS for ?
The below form elements are also supported:
Basic semantics
Explaining the DOMFirst, obviously, there is a Form element with the novalidate attribute to make sure to disable the native HTML5 validations (which currently suck). proceeding it there is a Fieldset element which is not a must, but acts as a ?binding? box for a group of fields that are under the same ?category?. For bigger forms there are many times field groups that are visually separated from each other for example. Now, we treat every form field element the user interacts with, whatsoever, as an ?item?, and therefor these ?items? will be wraped with The whole approach here is to define each form field (input, select, whatever) as much as possible with HTML5 attributes and also with custom attributes. | Attribute | Purpose |
|----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| required | Defines that this field should be validated (with JS by my implementation and not via native HTML5 browser defaults) |
| placeholder | Writes some placeholder text which usually describes the fields with some example input (not supported in IE8 and below) |
| pattern | Defines a pattern which the field is evaluated with. Available values are:<br>numeric - Allow only numbers<br>alphanumeric - Allow only numbers or letters. No special language characters<br>phone - Allow only numbers, spaces or dashes.<br><br>Alternatively, you may write your own custom regex here as well. |
| data-validate-words | Defines the minimum amount of words for this field |
| data-validate-length | Defines the length allowed for the field (after trim). Example value: Optional fieldsThere is also support for optional fields, which are not validated, unless they have a value. The support for this feature is done by adding a class ?optional? to a form element. Note that this should not be done along side the ?required? attribute. Error messagesThe validator function holds a messages object called "message", which itself holds all the error messages being shown to the user for all sort of validation errors.
This object can be extended easily. The idea is to extend it with new keys which represent the name of the field you want the message to be linked to, and that custom message appear as the
Error messages can be disabled:
Binding the validation to a formThere are 2 ways to validate form fields, one is on the submit event of the form itself, then all the fields are evaluated one by one. The other method is by binding the ?checkField? function itself to each field, for events like ?Blur?, ?Change? or whatever event you wish (I prefer on Blur). Usage example - validate on submitA generic callback function using jQuery to have the form validated on the Submit event. You can also include your own personal validations before the checkAll() call.
Usage example - validate on field blur event (out of focus)Check every field once it looses focus (onBlur) event
TooltipsThe helper tooltips <div class='tooltip help'>, which work using pure CSS, are element which holds a small '?' icon and when hovered over with the mouse, reveals a text explaining what the field ?item? is about or for example, what the allowed input format is. Classes
Bonos ? multifieldsI have a cool feature I wrote which I call ?multifields?. These are fields which are often use as to input a credit card or a serial number, and are actually a bunch of input fields which are ?connected? to each other, and treated as one. You can see it in the demo page, and it?s included in ?multifield.js? file. |