通用校验

var validate = (function() { var instructions = { notEmpty: "不能为空!", isPhone: "手机号不正确!" }; var types = { notEmpty: function(value) { if(value == null || value.length === 0) { return false; } return true; }, //校验手机号 isPhone: function(value) { // var reg = /\\d+/; var reg = /^0?1[3|4|5|7|8][0-9]\d{8}$/; if(reg.test(value)) { return true; } return false; } } return function(value, type) { //type为检测类型,value为检测的值 if(!types[type]) { throw "检测类型不存在"; } if(!types[type](value)) { return instructions[type]; } return false; } })(); var Detect = function() { this.result = []; } Detect.prototype.add = function(value, types) { if(!(types instanceof Array)) { throw "检测类型只能为数组"; } for(var type of types) { var msg = validate(value, type); if(!!msg) { this.result.push(msg); } } } Detect.prototype.getResult = function() { var result = this.result; return result.length ? result : false; } var detect = new Detect(); detect.add("", ["notEmpty"]); //添加值的验证 detect.add(123, ["isPhone"]); //添加另外一个值的验证 console.log(detect.getResult());

你可能感兴趣的:(通用校验)