@bravf
2015-12-17T02:35:10.000000Z
字数 1947
阅读 1007
未分类
import mode:
//script link<script src="small-validator.js"></script>//commonjsvar SmallValidator = require('small-validator.js')//amddefine(['small-validator.js'], function (){})
sometimes, we have a user form like this:
<form class="js-test-form"><input type="text" class="js-user"/><input type="password" class="js-password"><button class="js-submit-btn">提交</button></form>
we can valid the form with small-validator:
var SmallValidator = require('small-validator.js')//create a form controlvar formControl = new SmallValidator.control()//create user controlvar userControl = SmallValidator.control('.js-user')// add user rulesvar notEmptyRule = SmallValidator.required('username is required')var lengthRule = SmallValidator.rule(/^*{5,8}$/, 'username length should in 5..8')var userExistsRule = SmallValidator.rule('userExists.php', function (control, data){if (data.exist){return false}else {return true}}, 'user already exist')userControl.add(notEmptyRule, lengthRule, userExistsRule)//create password controlvar passControl = SmallValidator.control('.js-password')//add password rulesvar notEmptyRule = SmallValidator.required('password is required')var lengthRule = SmallValidator.rule(function (control){var value = control.val()return /^\d{5,8}$/.test(value)}, 'password length should in 5..8 and all chars should be number')passControl.add(notEmptyRule, lengthRule)//add controls to formformControl.add(userControl, passControl)//bind event$('.js-submit-btn').on('click', function (){formControl.check().done(function (){//when valid ok}).fail(function (){//when valid fail})})
actually, a more simple method:
var formControlwith (SmallValidator){formControl = control().add(//usercontrol('.js-user').add(required('user is required'),length([5, 8], 'username length should in 5..8'),rule('userExists.php', function (control, data){if (data.exist){return false}else {return true}}, 'user already exist')),//passwordcontrol('.js-password').add(required('password is required'),rule(/^\d{5,8}$/, 'password length should in 5..8 and all chars should be number')))}
read soure code and enjoy it!