API
Using the plugin:
$(form).djValidator([mode],[callback])
It should be invoked on forms or form fields.
Parameter
|
Type
|
Description
|
mode
|
String
|
Optional, is the mode in which the validator will be used, if not
placed by default it is attached to the submit event of the forms.
Other possible values are:
• 'callback': It is attached to the submit event of the form and calls
the function passed in the parameter [callback] when all fields are valid.
• 'validate': Valid a form as a function and returns true or false,
used when sending the form by javascript (only invoked on a form).
• 'field': invoked on a form field to validate
it, returns true or false (only invoked on a field).
• ‘clean’ (v1.1.0) : Borra todos los mensajes de validación colocados en los formularios. |
callback
|
function
|
Optional, only specified when the [mode] parameter is 'callback', is the function that must be executed when the form is already valid, this function should receive the form as a parameter.
|
Examples:
By default to submit event:
$('form').djValidator();
To the submit event, execute the function when it is valid:
$('form').djValidator('callback', function($form){
//code
$form.submit();
});
As function:
$ ('#myform').djValidator('validate');
As a function, validate only one field:
$ ('#myInput').djValidator('field');
$('form').djValidator();
To the submit event, execute the function when it is valid:
$('form').djValidator('callback', function($form){
//code
$form.submit();
});
As function:
$ ('#myform').djValidator('validate');
As a function, validate only one field:
$ ('#myInput').djValidator('field');
Erase validation messages:
$(‘#myform’).
djValidator(‘clean’);
Add a new validator inside DjValidator:
$.addDjvalidator(key,msg,callback);
Parameter
|
Type
|
Description
|
key
|
String
|
Name of the key to use.
|
msg
|
String
|
Validation message.
|
callback
|
function
|
Function that performs validation, receives 2 parameters:
[$field]: The field to validate (object). [args]: The validation parameters (array of strings). The function must return true or false. |
Example:
$.addDjValidator('even','only even numbers.',function($field,args){
value=parseInt($field.val());
max=args[1];
if(value%2!=1||value>max)return false;
return true;
});
Change the CSS style of the validation messages:
$.setDjvalidatorStyle(style);
Parameter
|
Type
|
Description
|
style
|
String
|
Contains the styles to apply in the "style" attribute of the messages, you must add "display: none;" to see a FadeIn effect.
|
Example:
$.setDjValidatorStyle('display:none; text-align:center; color:white; background-color:red;');
Change DjValidator validation messages to customize or translate:
$.setDjValidatorLabels(labels);
Parameter
|
Type
|
Description
|
labels
|
Object
|
Contains names and messages in json format, replaceable parameters are preceded by the $ sign.
See the Spanish translation as a reference of the names of the messages. |
Example (See in the Spanish translation the complete example):
$.setDjValidatorLabels({
required:'Campo requerido.',
word_min:'Por lo menos $1 caracteres sin espacios.',
word_between:'Entre $1 y $2 caracteres sin espacios.',
…………………………… });
Attributes to use
Separators recognized in a validation rule
The following characters are special within the validation key (data-dj-validator attribute):
Attributes to use
The following attributes are used by DjValidator in the form fields:
Utility
|
Attribute
|
Description
|
Example
|
Validator to use
|
data-dj-validator
|
Add validation to the field with the specified key (see validation key table).
|
data-dj-validator="text,3,12"
|
Relate fields
|
data-dj-validator-group
|
Specifies a group of related fields by the same attribute value.
|
data-dj-validator-group=”lenguajes”
|
Validation message
|
data-dj-validator-msg
|
Defines a custom validation message for the field.
|
data-dj-validator-msg=”Name is not valid!”
|
Field required
|
required
|
The required HTML5 attribute.
|
required
|
Message in compound fields
(v1.1.0) | data-input-group | Place the message as a child of the parent element of the field, used to correct the position of the message in fields composed of several elements. The value used is irrelevant. | data-input-group="group" |
Separators recognized in a validation rule
The following characters are special within the validation key (data-dj-validator attribute):
Utility
|
Character
|
Description
|
Example
|
Separate validators
|
&
|
Separate multiple validation keys.
|
“atext,3,12&word,3,12”
|
Separate parameters
|
,
|
Separate the parameters of a validation key
|
“Text,3,50”
|
Separate values from an array-type validation parameter
|
|
|
Separates the values of a multi-valued parameter.
|
“efile,jpg|jpeg|png|bmp”
|
Omit parameter
|
*
|
Omits the parameter validation as long as the key allows it (see table of validators).
|
“atext,5,*”
|
Recognize commas (v1.1.0)
| ° | Since the comma is a special character for the validator, if regular expressions are used with commas in the "regexp" validator, they must be replaced with this character. | "regexp,^[aeiou]{3°5}$,i" |
No hay comentarios:
Publicar un comentario