zoukankan      html  css  js  c++  java
  • 分享一个表单校验的验证规则

    直接看正文

    /**
    数据验证(表单验证)
    来自 grace.hcoder.net 
    作者 hcoder 深海
    */
    module.exports = {
    	error:'',
    	check : function (data, rule){
    		for(var i = 0; i < rule.length; i++){
    			if (!rule[i].checkType){return true;}
    			if (!rule[i].name) {return true;}
    			if (!rule[i].errorMsg) {return true;}
    			if (!data[rule[i].name]) {this.error = rule[i].errorMsg; return false;}
    			switch (rule[i].checkType){
    				case 'string':
    					var reg = new RegExp('^.{' + rule[i].checkRule + '}$');
    					if(!reg.test(data[rule[i].name])) {this.error = rule[i].errorMsg; return false;}
    				break;
    				case 'int':
    					var reg = new RegExp('^(-[1-9]|[1-9])[0-9]{' + rule[i].checkRule + '}$');
    					if(!reg.test(data[rule[i].name])) {this.error = rule[i].errorMsg; return false;}
    					break;
    				break;
    				case 'between':
    					if (!this.isNumber(data[rule[i].name])){
    						this.error = rule[i].errorMsg;
    						return false;
    					}
    					var minMax = rule[i].checkRule.split(',');
    					minMax[0] = Number(minMax[0]);
    					minMax[1] = Number(minMax[1]);
    					if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
    						this.error = rule[i].errorMsg;
    						return false;
    					}
    				break;
    				case 'betweenD':
    					var reg = /^-?[1-9][0-9]?$/;
    					if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
    					var minMax = rule[i].checkRule.split(',');
    					minMax[0] = Number(minMax[0]);
    					minMax[1] = Number(minMax[1]);
    					if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
    						this.error = rule[i].errorMsg;
    						return false;
    					}
    				break;
    				case 'betweenF': 
    					var reg = /^-?[0-9][0-9]?.+[0-9]+$/;
    					if (!reg.test(data[rule[i].name])){this.error = rule[i].errorMsg; return false;}
    					var minMax = rule[i].checkRule.split(',');
    					minMax[0] = Number(minMax[0]);
    					minMax[1] = Number(minMax[1]);
    					if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
    						this.error = rule[i].errorMsg;
    						return false;
    					}
    				break;
    				case 'same':
    					if (data[rule[i].name] != rule[i].checkRule) { this.error = rule[i].errorMsg; return false;}
    				break;
    				case 'notsame':
    					if (data[rule[i].name] == rule[i].checkRule) { this.error = rule[i].errorMsg; return false; }
    				break;
    				case 'email':
    					var reg = /^w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*$/;
    					if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
    				break;
    				case 'phoneno':
    					var reg = /^1[0-9]{10,10}$/;
    					if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
    				break;
    				case 'zipcode':
    					var reg = /^[0-9]{6}$/;
    					if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
    				break;
    				case 'reg':
    					var reg = new RegExp(rule[i].checkRule);
    					if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
    				break;
    				case 'in':
    					if(rule[i].checkRule.indexOf(data[rule[i].name]) == -1){
    						this.error = rule[i].errorMsg; return false;
    					}
    				break;
    				case 'notnull':
    					if(data[rule[i].name] == null || data[rule[i].name].length < 1){this.error = rule[i].errorMsg; return false;}
    				break;
    			}
    		}
    		return true;
    	},
    	isNumber : function (checkVal){
    		var reg = /^-?[1-9][0-9]?.?[0-9]*$/;
    		return reg.test(checkVal);
    	}
    }
    
  • 相关阅读:
    Spring Boot构建RESTful API与单元测试
    Spring Boot中使用Swagger2构建强大的RESTful API文档
    Intellij IDEA 一些不为人知的技巧
    Spring中@Controller和@RestController之间的区别
    Spring 中的default-lazy-init="true" 和 lazy-init="true"
    SpringMVC处理JSON
    建立一个简单的SpringMVC程序
    SpringMVC处理静态资源
    <mvc:annotation-driven/>与<mvc:default-servlet-handler/>之间的一个问题
    Spring AOP 简单理解
  • 原文地址:https://www.cnblogs.com/codebook/p/12586472.html
Copyright © 2011-2022 走看看