Created
June 4, 2017 09:15
-
-
Save vino24/dc7a027e37575fe7a3acf71dc17b3216 to your computer and use it in GitHub Desktop.
策略模式实现表单验证
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 策略对象 | |
const strategies = { | |
isNonEmpty(value, errorMsg) { | |
return value === '' ? | |
errorMsg : void 0 | |
}, | |
minLength(value, length, errorMsg) { | |
return value.length < length ? | |
errorMsg : void 0 | |
}, | |
isMoblie(value, errorMsg) { | |
return !/^1(3|5|7|8|9)[0-9]{9}$/.test(value) ? | |
errorMsg : void 0 | |
}, | |
isEmail(value, errorMsg) { | |
return !/^\w+([+-.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value) ? | |
errorMsg : void 0 | |
} | |
} | |
//Validator类 | |
class Validator { | |
constructor() { | |
this.cache = [] //保存校验规则 | |
} | |
add(dom, rules) { | |
for (let rule of rules) { | |
let strategyAry = rule.strategy.split(':') //例如['minLength',6] | |
let errorMsg = rule.errorMsg //'用户名不能为空' | |
this.cache.push(() => { | |
let strategy = strategyAry.shift() //用户挑选的strategy | |
strategyAry.unshift(dom.value) //把input的value添加进参数列表 | |
strategyAry.push(errorMsg) //把errorMsg添加进参数列表,[dom.value,6,errorMsg] | |
return strategies[strategy].apply(dom, strategyAry) | |
}) | |
} | |
} | |
start() { | |
for (let validatorFunc of this.cache) { | |
let errorMsg = validatorFunc()//开始校验,并取得校验后的返回信息 | |
if (errorMsg) {//r如果有确切返回值,说明校验没有通过 | |
return errorMsg | |
} | |
} | |
} | |
} | |
// 调用 | |
let registerForm = document.querySelector('#registerForm') | |
const validatorFunc = () => { | |
let validator = new Validator() | |
validator.add(registerForm.userName, [{ | |
strategy: 'isNonEmpty', | |
errorMsg: '用户名不能为空!' | |
}, { | |
strategy: 'minLength:6', | |
errorMsg: '用户名长度不能小于6位!' | |
}]) | |
validator.add(registerForm.passWord, [{ | |
strategy: 'isNonEmpty', | |
errorMsg: '密码不能为空!' | |
}, { | |
strategy: 'minLength:', | |
errorMsg: '密码长度不能小于6位!' | |
}]) | |
validator.add(registerForm.phoneNumber, [{ | |
strategy: 'isNonEmpty', | |
errorMsg: '手机号码不能为空!' | |
}, { | |
strategy: 'isMoblie', | |
errorMsg: '手机号码格式不正确!' | |
}]) | |
validator.add(registerForm.emailAddress, [{ | |
strategy: 'isNonEmpty', | |
errorMsg: '邮箱地址不能为空!' | |
}, { | |
strategy: 'isEmail', | |
errorMsg: '邮箱地址格式不正确!' | |
}]) | |
let errorMsg = validator.start() | |
return errorMsg | |
} | |
registerForm.addEventListener('submit', function() { | |
let errorMsg = validatorFunc() | |
if (errorMsg) { | |
alert(errorMsg) | |
return false | |
} | |
}, false) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment