boss.validator
The simplest library to validate data or forms.
Send!
Index
Installation
npm
npm install boss.validator
bower
bower install boss.validator
CDN
Validators
Defaults
- required
Numbers
- less
- less_equal
- bigger
- bigger_equal
- between
Strings
- exact
- minlength
- maxlength
- extensions - For upload inputs
- starts
- ends
- contains
Booleans
- boolean
Regex
- regex - Create a custom regex
- url
- https
- credit_card
- ip_v4
- ip_v6
- alpha_numeric
- alpha
Messages
For each validator, also have a error message. There is also a default message to cases where doesn't have a message to a validator, this is the default
and you can override it.
const messages = default: 'Please, fill this field.' required: 'This field is required.' // Numbers, Sizes less: 'The value needs to be less than {val}.' less_equal: 'The value needs to be less than or equal to {val}.' bigger: 'The value needs to be bigger than {val}.' bigger_equal: 'The value needs to be less than or equal to {val}.' between: 'The value must be between {val}' number: 'Please, enter a valid number.' // Strings exact: 'Please, this field needs to have {val} characters.' extensions: 'Please, upload a file with some of these extensions: {val}.' contains: 'Please, this field needs to have the value: {val}.' minlength: 'Please, this field needs minimum {val} characters.' maxlength: 'Please, this field needs maximum {val} characters.' starts: 'Please, this field needs to start with "{val}".' ends: 'Please, this field needs to end with "{val}".' // Booleans boolean: 'This field needs to be "true" or "false".' // Regex email: 'Please, provide a valid email address.' url: 'Please, provide a valid URL address with http:// or https://.' https: 'Your URL must starts with https://' credit_card: 'Please, enter a valid credit card number.' ip_v4: 'Please, enter a valid IPV4 address.' ip_v6: 'Please, enter a valid IPV6 address.' alpha: 'Only alpha characters are allowed.' alpha_numeric: 'Only alpha numeric characters are allowed.';
Methods
Boss.validate(form, rules)
Boss.validate(object, rules)
For each object in object
, it needs to have the value
property.
let fields = ip: value: '192.168.0.1' ; let rules = ip: ip_v4: true ; Boss;
Boss.configure(object)
// Default valuesBoss;
Boss.configureMessages(object)
You can simply override them or create new.
Boss;
Boss.addValidator(object)
Power to create custom validators. Easy.
Boss; Boss;
You can create a validator and pass its message too.
Boss; Boss;
Boss.setErrorClass(className)
Boss;
TODO
Why not to help?
- 🚀
- Publish on
npm install -i boss.validator
andbower install bower.validator
- Improve and review the messages
- Create a easy way to extend I18n messages
- Create unit tests !important
- Create more useful validators
- Improve this documentation
- Create gh-pages branch
- Create a logo
Browser Support
The boss.validators
needs to support these features:
Object.keys
Promise
String.prototype.endsWith
Element.prototype.classList
Object.assign
If you aren't sure about these features, please, use this smart polyfill:
Made with ❤️ by community!