JAVASCRIPT
Form Validation
All samples are using Ink.requireModules
, please read how to use it at Ink.requireModules section
Function name | Description |
---|---|
.reset() | Resets previously generated validation errors |
.validate(elm, [options]) | Checks if a form is valid |
Property name | Description |
---|---|
.confirmElms | This property holds the objects needed to cross-check for the 'confirm' rule |
.elements | This property holds all form elements for later validation |
.hasConfirm | This property holds the previous elements in the confirmElms property, but with a |
.version | Specifies the version of the component |
Resets previously generated validation errors
Checks if a form is valid
elm
DOM form element or form idoptions
Configuration optionsoptions.onSuccess
Callback to run when form is validoptions.onError
Callback to run when form is not validoptions.customFlag
Custom flags to use to validate form fieldsoptions.confirmGroup
Besides the markup structure you see in
Forms, you need to add
the ink-fv-*
classes to your inputs, which correspond to validation
rules in this component. Available classes are:
ink-fv-required
: Required fieldink-fv-email
: Valid e-mailink-fv-url
: Valid URL addressink-fv-number
: Valid numberink-fv-phone_pt
, ink-fv-phone_cv
, ink-fv-phone_mz
, ink-fv-phone_ao
: Valid telephone number in Portugal, Cape Verde, Mozambique or Angola.ink-fv-date
: Valid dateink-fv-confirm
: Make the user type the same thing twice. Common rule for confirming passwords.ink-fv-custom
: Custom rule (see below example "Custom rule")For example:
<input class="ink-fv-required ink-fv-email"><br>
<input class="ink-fv-number ink-fv-required"><br>
<input class="ink-fv-url">
So you have a form and would like to validate it? This example shows how to validate a form and stop it from being submitted when invalid. To use this, add the several ink-fv-* classes to your input elements.
The validate() function will also add "invalid" classes to each of your elements so the user gets a color feedback and an error message below each element (control-group, really).
The following form has a required number field. You must input a number.
Demo
Code
The following code validates using a custom rule named minthree
,
which fails if the input string has less than three characters.
To do this, you must add the ink-fv-custom
and minthree
classes
to the input elements you want to validate, and pass the customFlag
option to this function, like so:
Demo
Code
Object
This property holds the objects needed to cross-check for the 'confirm' rule
Object
This property holds all form elements for later validation
Object
This property holds the previous elements in the confirmElms property, but with a
true/false specifying if it has the class ink-fv-confirm.
String
Specifies the version of the component