Form-amd library
form.js is a small library to help form manipulations and validation. This library uses amd structure.
It uses html5 form attributes validate specification and works on browsers that does not support html5 validation. Then, we built it from scratch.
Install with npm: npm install elo7-form-amd
Form-amd depends on an amd implementation. We suggest async-define implementation for dependency lookup. Form-amd also depends on doc-amd.
.submitOnChange(selectorOrDocElement[, callback])
Submit the parent form when event change is triggered.
selectorOrDocElement: doc-amd object or String //A CSS selector. Note that, if it is a class name with dots, the dots must be escaped. E.g.: doc(".my\\.class")
callback: Function() //A function to call before the event is triggered
define(['form'], function(form) {
form.submitOnChange($('#country')); //Submit the parent form when the country is selected
form.submitOnChange('#country', function(){...}); //Run the callback function and then submit the parent form when the country is selected
});
.submitOnBlur(selector)
Submit the parent form when event blur is triggered.
selector: String
define(['form'], function(form) {
form.submitOnBlur('#name'); //Submit the parent form when the form element loses focus
});
.focus(selector)
Focus on selected element. If the device is mobile, it calls scrollIntoView function.
selector: String
define(['form'], function(form) {
form.focus('#input'); //Focus on the element #input
});
.validate(selectorOrDocElement[, object])
Validate the form using almost all the html5 attributes validate spec.
selectorOrDocElement: doc-amd object or String //A CSS selector. Note that, if it is a class name with dots, the dots must be escaped. E.g.: doc(".my\\.class")
object: Object //An object with the properties messages ("required", "min", "maxlength", "pattern" or "email"), success (function callback) or error (function callback)
define(['form'], function(form) {
form.validate($('#form')); //Validate the form with default messages
form.validate('#form', {
messages: {
'required': 'Field required.',
'min': 'Enter a value greater than or equal to {0}.',
'maxlength': 'Enter a value with max length less than or equal to {0}.',
'pattern': 'Enter a valid value.',
'email': 'Enter a valid email address.'
}, //Validate the form with this messages
success: function(){
// success callback
},
error: function(){
// error callback
}
});
});
required: This field is required
min: Please enter a value greater than or equal to {0}
maxlength: Please enter a value with max length less than or equal to {0}
pattern: Please enter a valid value
email: Please enter a valid email address
.append(selector, text)
Append validation messages
selector: String
text: String
define(['form'], function(form) {
form.append('label[for="date"]', 'dd/mm/yyyy'); //This will append <span class="message">dd/mm/yyyy</span>. Note that this element will be removed when the user starts to type another value.
});
.removeValidationErrors(selector)
Removes all validation messages from selected form
selector: String
define(['form'], function(form) {
form.removeValidationErrors('#form'); //This will remove all validation messages appended
});
Form-amd is released under the BSD. Have at it.
Copyright ©️ 2019 Elo7# form-amd