This library help us to validate the form as well as model.
You will need jQuery in order to use this library.
npm install jquery
You can get the library by using following command:
npm install fmvalidation
var layout = [
selector: '.username',
rules: {
required: true,
},
message: {
required: 'Username is required'
}
]
var validate = new Validation({
layout: layout
}, $('form'));
var layout = [{
identifier: <string>
path: <string>,
selector: <string>,
rules:{
required: <values for validation>,
<custom_rule>: <function>
}
message: {
required: <string>,
<custom_rule>: <string>
}
getValue: <string/function>
showError: <function>
hideError: <function>
errorContainer: <string>
}]
var validate = new Validation({
layout: layout
errorOnSubmit: <boolean>,
errorOnChange: <boolean>,
defaultError: <boolean>,
model: <object>,
showAllErrors: <function>,
hideAllErrors: <function>,
errorContainer: <string>,
}, $('form'));
It will unqiue identifier if you want to validate only the particular selector. ex: validate.validate()
We need to specify path or selector. If you need to valdiate form then selector should provided or if you want to validate model the path('test1.0.name') should be provided.
Rules are inbuilt rules or function that validate the form or object path.
Messages are shown when error occurs in validation for rules.
If we are not able to read value from element from form or object we can return value which will be used for validation
If we want to show and hide error in our way we can use these.
Display error in specific element.
By default its true. If its true, validation will happen when user is typing.
By default its false. If its true, validation will happen when user change the value. It will overwrite the defaultError.
By default its false. If its true, validation will happen when user call validate or form sumbit. It will overwrite the defaultError.
If we want to show and hide all errors in our way we can use these.
If we have model based approach and can't valdaite the form, we can pass an object on which we have to do validation.
It will validate and display error if any. It will return true if no validation error occurs. If identifier is present it will valid only that rule.
It will only valdiate the form or object but will not through error.
It will add rule at runtime.
It will remove rule at runtime.
Reset all errors
It will add validation rule globally and we can you in our rules -> layout;
required - true
name - true
number - true
decimal - true
username - true
password - true
email - true
url - true
date - true
equalTo - selector for other element
minLength - length
maxLength - length
rangeLength - [min, max]
minValue - value
maxValue - value
rangeValue - [min, max]
valueEqualTo - string/array
valueNotEqualTo - string/array