123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- /* eslint-disable */
- /**
- *@des 表单验证
- *@author hjp1011 21931118@qq.com
- *@blog http://www.wephp.com
- *@date 2019/11/15 10:55:56
- */
- module.exports = {
- error: '',
- check: function(data, rule) {
- for (let 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 = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/;
- 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) {
- let reg = /^-?[1-9][0-9]?.?[0-9]*$/;
- return reg.test(checkVal);
- }
- };
|