-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the native-form-validation
wiki!
There is a lot of Form Validation
, but mostly of them was created for jQuery
and too bloated.
So here is the reason why native-form-validation
was created:
- Simple and Customizable.
- Lightweight (minified and gzipped size only 1Kb).
- No hidden or silent magic in submit.
- Native (work for most all JavaScript UI framework).
- Build to follow ES5 Standard (support for old browser).
$ npm install native-form-validation
// load using require in nodejs
const FormValidation = require('native-form-validation');
// or load using import in typescript
import * as FormValidation from 'native-form-validation';
// or load use with path for client side
<script src="node_modules/native-form-validation/dist/formvalidation.min.js"></script>
Or simply use with CDN
<!-- Always get the latest version -->
<!-- Not recommended for production sites! -->
<script src="https://cdn.jsdelivr.net/npm/native-form-validation/dist/formvalidation.min.js"></script>
<!-- Get minor updates and patch fixes within a major version -->
<script src="https://cdn.jsdelivr.net/npm/native-form-validation@1/dist/formvalidation.min.js"></script>
<!-- Get patch fixes within a minor version -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/formvalidation.min.js"></script>
<!-- Get a specific version -->
<!-- Recommended for production sites! -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/formvalidation.min.js"></script>
var form = new FormValidation();
// Set the rules
form.rules({
username: {
required: true,
message: 'Username must be min 3-20 chars!',
minLength:3,
maxLength:20,
regex: /^[a-zA-Z0-9]/,
errorPlace:'username-error',
errorAddClass: {
username_group:'has-danger',
username:'is-invalid'
}
},
email: {
regex: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
errorPlace:'email-error',
errorAddClass: {
email_group:'has-danger',
email:'is-invalid'
}
}
});
// Validate all
form.validate();
// Determine is Valid all
if(form.isValid()) {
// run your code
}
// Validate per element
form.element('username').validate();
// Determine is Valid per element
if(form.element('username').isValid()) {
// run your code
}
- form.rules(options) - Set the rules for validation.
-
Options in form.rules) - Describe the available options in
form.rules()
. - form.validate(callback) - Make validation.
- form.isValid() - Determine if validation has valid or not.
- form.element(id) - Make validation for single element.
- form.reset() - Reset the form validation automatically.
First we have to create the rules for form validation.
Example:
form.rules({
username: {
required: true,
message: 'Username must be min 3-20 chars!',
minLength:3,
maxLength:20,
errorPlace:'username-error',
errorAddClass: {
username_group:'has-danger',
username:'is-invalid'
}
});
Based from example above, now we explain the available options in form.rules()
.
- element {object} : Create rules for element. If you want to set rules for username see above example.
-
required {bool} : Make sure the input is not empty. Default is
false
. [optional
] -
trim {bool} : Sanitize space and whitespace on first and tail. Default is
true
. [optional
] -
message {string} : Customize the error text message. [
optional
] -
minLength {int} : Make sure the input has {
minLength
} of chars. [optional
] -
maxLength {int} : Make sure the input not more than {
maxLength
} of chars. [optional
] -
errorPlace {string} : Display the error text into spesific element. [
optional
] -
errorAddClass {object} : Add custom error class into element. [
optional
] -
regex {regex} : Create validation based on regex. See this example. [
optional
] -
method {function} : Create custom validation without regex. See this example. [
optional
]
Make validation based on rules.
-
callback(error) : Callback is to show the error validation. [
optional
]
Example:
form.validate(function(error) {
if(error) {
// you are able to customize the element style form based on error validation object.
}
});
Determine if validation has valid or not.
Example:
if(form.isValid()) {
// call ajax submit
}
Make validation for single element. If you have set the rules for many element then if you want to validate for single element, for example if you want to play around in event.
- id {string} : the name of element id.
Example:
// Vanilla
var input = document.getElementById('username');
input.addEventListener('blur', function(e){
form.element('username').validate();
});
// jQuery
$('#username').on('blur', function() {
form.element('username').validate();
});
This will reset all error message on your form. Example:
form.reset();
I have made simple live example with Bootstrap 4
. Hope this will make easier you to understand. Check this link JSFiddle