Validator
Validator是一个基于jQuery的验证器,验证成功后在回调函数中返回验证的健值对儿,极大的方便了表单验证+提交
- Live DEMO http://klamtlne.github.io/Validator/
- Github https://github.com/klamtlne/Validator
- 欢迎issuses,PR,接下来准备增加验证类型和自定义验证条件,以及更加友好的报错
Usage
<form id="form">
<input type="text" data-validator="name" data-rules="notEmpty, maxLength=10">
<input type="password" data-validator="password" data-rules="notEmpty, minLength=6">
<input type="email" data-validator="email" data-rules="notEmpty, email">
<input type="text" data-validator="phone" data-rules="phone">
<input type="checkbox" data-validator="fruit" value="apple" data-rules="atLeast=1, atMost=2">
<input type="checkbox" data-validator="fruit" value="pear">
<input type="checkbox" data-validator="fruit" value="lemon">
<select data-validator="selection" data-rules="notEmpty">
<option value="1"></option>
<option value="2"></option>
</select>
</form>
var config = {
msg: {
name: {
notEmpty: "name can’t be empty",
maxLength: "name can't be more than 10 characters"
},
password: {
minLength: "password can’t be less than 6 characters"
}
// other messages goes here...
}
}
$("#form").valdiate(config, function (result, data) {
// if success: result = {pass: true}
// data = {name: xxx, password: xxx, email: xxx...}
// if fail: {pass: false, msg: xxx}
// data = undefined
console.log(result)
})