Universal Validator.
via spm:
$ spm install univ
via npm:
$ npm install univ
var Validator = require('univ');
var rules = {
"username": {
type: "email",
required: true,
custom: function(value){
// Async validate.
return new Promise(function(resolve, reject){
$.post("/check-username-available", {username:value}, function(result) {
resolve(result.available === "YES")
}, "json")
})
}
},
"password": {
type: "password",
required: true,
minlength: 6,
maxlength: 30
},
"re-password": {
type: "password",
required: true,
minlength: 6,
maxlength: 30,
custom: function(value){
// Sync validate.
return value === this.data("password");
}
}
};
var validator = new Validator(rules);
var result = yield validator.validate({
"username": "hotoo@email.address",
"password": "PassWord",
"checkbox": ["check-0", "check-1"]
});
constructor, new a validator by rulers.
{
// rule name.
"name": {
type: {TypeEnum(
text,password, search,textarea,
radio,checkbox,
select-one,select-multiple,
number,range, email,url,tel,color,
date,week,month,time,datetime,datetime-local,
file,
submit,button,image,
hidden
)},
required: {Boolean},
max: {Number},
min: {Number},
maxlength: {Number},
minlength: {Number},
pattern: {RegExp},
multiple: {Boolean},
step: {Number},
accept: {Array<String>},
custom: {Function}
},
"other-name": {
// ...
},
// ...
}
Set or get a custom rule.
validator.rule("isBankCard", function(values){
return true;
});
data:
univ.validate(
{
"name": "value",
"other-name": ["item-1", "item-2"],
// ...
}
);
Set field is required or not.
Set field type, type value is a enum list(same html5 type):
- text
- password
- search
- textarea
- radio
- checkbox
- select-one
- select-multiple
- number
- range
- url
- tel
- color
- date
- week
- month
- time
- datetime
- datetime-local
- file
- submit
- button
- image
- hidden
- For
[type=number]
, input data must be great than the min value. - For
[type=date]
and so on, input data
单个数据项通过校验,数据合法有效。
validator.on("valid", function(name, value, validity){
console.log("Field [name=" + name + "] Passed.");
});
单个数据项未通过校验,数据无效、不合法。
validator.on("valid", function(name, value, validity){
console.log("Field [name=" + name + "] Failed.");
});
所有数据校验完成。数据是否全部通过校验,则视事件处理函数的对应参数。
// @param {Boolean} certified.
validator.on("complete", function(certified){
console.log("Form validation", certified ? "Passed" : "Failed");
});
校验过程中出现异常,则抛出 error
事件。
// @param {Error} error
validator.on("error", function(error){
});