页面项共通校验validate

news/2024/7/19 15:26:37 标签: validate, js


原生的校验插件不太好用,故项目中重写了validate.js文件!


下面为具体的用法步骤:


1. 需要实现的页面效果



2. 页面写法

<li><span><i class="redstar">*</i>重量</span>
      <input type="text" name="loadLimit" class="input-txt w-s94" validate="required digits[8]"/> 吨
</li>

3.  重量前面的必填标记css

.redstar{
	display: inline-block;
	vertical-align: top;
	color: #ff2400;
}


4. 提交表单方法里面调用

// formid 表单id,前面不需要加"#"号
var result = checkSingleForm(formid);
if (!result) return;  //校验不通过返回

5. 引入validate.js


6. validate.js内容如下:

$('input,textarea,select').blur(function(){
	var $this = $(this);
	return validates.checkvalidate($this);
});

$('select').change(function(){
	var $this = $(this);
	return validates.checkselect($this);
});


//提交时验证单独form表单
function checkSingleForm(id) {
	var istrue = true;
	$('#'+id+' input,#'+id+' textarea,#'+id+' select').each(function(i,val){
		var $this = $(val);
		istrue = validates.checkvalidate($this);
		if(!istrue) return false;
	});
	if(!istrue) {
		return false;
	}
	$('#'+id+' select').each(function(i,val){
		var $this = $(val);
		istrue = validates.checkselect($this);
		if(!istrue) return false;
	});

	return istrue;
}

//提交时验证所有表单
function checksub() {
	var istrue = true;
	$('input,textarea,select').each(function(i,val){ 
		var $this = $(val);
		istrue = validates.checkvalidate($this);
		if(!istrue) return false;
	});
	if(!istrue) {
		return false;
	}
	$('select').each(function(i,val){ 
		var $this = $(val);
		istrue = validates.checkselect($this);
		if(!istrue) return false;
	});
	
	return istrue;
}

var validates = {
	$this:null,
	istrue:false,
	infos:{
		required:'必填字段,请输入',
		select2:'必填字段,请选择',
		maxlength:'超过最大长度限制',
		minlength:'小于最小长度限制',
		number:'只能输入整数',
		digits:'只能输入数字,小数点后保留两位小数',
		character:'只能输入英文字符',
		charnum:'只能输入字母和数字',
		charAndnum:'只能输入不少于6位字母和数字的组合',
		phone:'手机号码格式不正确',
		email:'邮箱格式不正确',
		select:'必填字段,请选择',
		dianhua:"电话的格式不正确",
		fax:"传真的格式不正确",
		equal:"输入的密码不一致",
	},
	init: function() { //初始化
		//alert("init");
		this.value = '';
		this.validate = '';
		this.$this=null;
	},
	checkvalidate: function(obj) {
		//alert("checkvalidate");
		this.init();
		this.validate = obj.attr('validate');
		if(this.empty(this.validate)) return true;
		this.$this = obj;
		if(this.has('select2')) {
			if(obj.val()==null){
				this.value = '';
			}else{
				this.value = this.tirm(obj.val()[0]);
			}

		}else{
			this.value = this.tirm(obj.val());
		}
		this.clear();
		//必填
		if(this.has('required')) {
			if(!this.required()) {
				return false;
			}
		}
		//必选
		if(this.has('select2')) {
			if(!this.select2()) {
				return false;
			}
		}
		
		//判断最大长度限制
		if(this.has('maxlength')) {
			if(!this.checkmaxlenth()) {
				return false;
			}
		}
		
		//判断最小长度限制
		if(this.has('minlength')) {
			if(!this.checkminlenth()) {
				return false;
			}
		}
		
		//判断数字int
		if(this.has('number')) {
			if(!this.checknumber()) {
				return false;
			}
		}
		
		//判断小数digits
		if(this.has('digits')) {
			if(!this.checkdigits()) {
				return false;
			}
		}
		
		//判断英文字符a-z A Z
		if(this.has('character')) {
			if(!this.checkcharacter()) {
				return false;
			}
		}
		
		//判断英文字符a-z A-Z 0-9
		if(this.has('charnum')) {
			if(!this.checkcharnum()) {
				return false;
			}
		}		
		//
		if(this.has('charAndnum')) {
			if(!this.checkcharAndnum()) {
				return false;
			}
		}		
		
		//判断手机号码
		if(this.has('phone')) {
			if(!this.checkphone()) {
				return false;
			}
		}
		
		//判断邮箱
		if(this.has('email')) {
			if(!this.checkemail()) {
				return false;
			}
		}
		//判断电话
		if(this.has('dianhua')){
			if(!this.checkdianhua()) {
				return false;
			}
	    }
		//判断传真
		if(this.has('fax')){
			if(!this.checkfax()) {
				return false;
			}
	    }
		//判断密码一致
		if(this.has('equal')){
			if(!this.checkequal()) {
				return false;
			}
		}
		return true;		
	},
	checkselect: function(obj) {
		//alert("checkvalidate");
		this.init();
		this.validate = obj.attr('validate');
		if(this.empty(this.validate)) return true;
		this.$this = obj;
		this.value = obj.val();
		this.clear();	
		if(this.empty(this.value)) {
			this.error(this.infos.select);
			return false;
		}
		return true;		
	},
	required: function() {//必填
		if(this.empty(this.value)) {
			this.error(this.infos.required);
			return false;
		}
		return true;
	},
	select2: function() {//必选
		if(this.empty(this.value)) {
			this.error(this.infos.select2);
			return false;
		}
		return true;
	},
	checkmaxlenth: function() {//最大长度
		if(this.empty(this.value)) return true;
		if(this.lenth('maxlength')) {
			var len = this.value.length;
			if(len>this['maxlength']) {
				this.error(this.infos.maxlength+this['maxlength']);
				return false;
			}
		}
		return true;
	},
	checkminlenth: function() {//最小长度
		if(this.lenth('minlength')) {
			var len = this.value.length;
			if(len<this['minlength']) {
				this.error(this.infos.minlength+this['minlength']);
				return false;
			}
		}
		return true;
	},
	checknumber: function() {//判断数字
		if(this.empty(this.value)) return true;
		var reg = /^[1-9]+[0-9]*$/;
		if(!reg.test(this.value)) {
			this.error(this.infos.number);
			return false;
		} 
		return true;
	},
	checkdigits: function() {//判断数字
		if(this.empty(this.value)) return true;
		var reg = /^[0-9]{1,15}(\.[0-9]{1,2})?$/
		if(!reg.test(this.value)) {
			this.error(this.infos.digits);
			return false;
		}
		if(this.lenth('digits')) {
			var len =0;
			if(this.value.indexOf('\.')<0){
				len=this.value.length;
			}else{
				len=this.value.substring(0,this.value.indexOf('\.')).length;
			}

			if(len>this['digits']) {
				this.error('整数位超过最大长度限制'+this['digits']);
				return false;
			}
		}
		return true;
	},
	checkcharacter: function() {//判断自还能由英文字符组成
		if(this.empty(this.value)) return true;
		var reg = /^[a-zA-Z]*$/
		if(!reg.test(this.value)) {
			this.error(this.infos.character);
			return false;
		} 
		return true;
	},
	checkcharnum: function() {//判断英文+数字
		if(this.empty(this.value)) return true;
		var reg = /^[a-zA-Z0-9]+$/
		if(!reg.test(this.value)) {
			this.error(this.infos.charnum);
			return false;
		} 
		return true;
	},
	checkcharAndnum: function() {//判断英文+数字
		if(this.empty(this.value)) return true;
		var reg = /(?=^.*?\d)(?=^.*?[a-zA-Z])^[0-9a-zA-Z]{6,}$/;
			if(!reg.test(this.value)) {
				this.error(this.infos.charAndnum);
				return false;
			} 
		return true;
	},
	checkphone: function() {//手机号码
		if(this.empty(this.value)) return true;
		var reg = /^1[34578]\d{9}$/
		if(!reg.test(this.value)) {
			this.error(this.infos.phone);
			return false;
		} 
		return true;
	},
	checkemail: function() {//邮箱
		if(this.empty(this.value)) return true;
		var reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
		if(!reg.test(this.value)) {
			this.error(this.infos.email);
			return false;
		} 
		return true;
	},
	checkdianhua: function() {//电话
		if(this.empty(this.value)) return true;
		var reg=/^((0\d{2,3})-{0,1})?(\d{7,8})(-(\d{1,}))?$/;
		if(!reg.test(this.value)) {
			this.error(this.infos.dianhua);
			return false;
		} 
		return true;
	},
	checkfax: function() {//传真
		if(this.empty(this.value)) return true;
		var reg=/^((0\d{2,3})-{0,1})?(\d{7,8})(-(\d{1,}))?$/;
		if(!reg.test(this.value)) {
			this.error(this.infos.fax);
			return false;
		} 
		return true;
	},
	checkequal:function () {
		if(this.empty(this.value)) return true;
		if(this.lenth('equal')) {
			var value = this.value;
			if(value!=$(this['equal']).val()) {
				this.error(this.infos.equal);
				return false;
			}
		}
		return true;
	},
	error: function(_info) {//必填
		this.$this.addClass('red-border');
		this.$this.parent().addClass('position-re');
		this.$this.after('<div class="poptip">'+_info+'<em><s class="s1">◆</s><s class="s2">◆</s></em></div>');
		var x = $('.poptip').offset().top-70;
		if($(window).scrollTop()>x+70){
			$('body,html').animate({scrollTop:x},500);
		}

	},
	clear: function() {//清空
		this.$this.removeClass('red-border');
		this.$this.parent().removeClass('position-re');
		$('.poptip').remove();
	},
	empty: function(_value) {//判断是否为空
		if(_value==undefined || _value==null || _value=="") {
			return true;
		}
		return false;
	},
	has: function(_info) {//是否包含
		if(this.empty(_info)) return false;
		if(this.validate.indexOf(_info)>-1) {
			return true;
		}
		return false;
	},
	tirm: function(_info) {//去除两边的空格
		if(this.empty(_info)) return '';
		return _info.replace(/(^\s*)|(\s*$)/g, "");
	},
	lenth: function(_info) {//获取maxlength 和minlength
		if(this.empty(_info)) return;
		var arr = this.validate.split(_info);
		if(arr.length<2) return; 
		var fnum = arr[1].indexOf("[");
		var fnum2 = arr[1].indexOf("]");
		if(fnum2<0 || fnum<0 || fnum>fnum2) return false;
		var maxlength = arr[1].substring(fnum+1,fnum2);
		this[_info] = maxlength;
		return true;
	}
};






http://www.niftyadmin.cn/n/1332762.html

相关文章

maven发布到私服

1. 最外层 pom.xml 中增加<distributionManagement><repository><id>xxxxx-dev</id><name>Nexus Release Repository</name><url>http://xxxxxxxxxxxxx:8081/nexus/content/repositories/xxxx/</url></repository><s…

JS中URL中的特殊字符问题:escape,encodeURI,encodeURIComponent

在使用url进行参数传递时&#xff0c;经常会传递一些中文名&#xff08;或含有特殊字符&#xff09;的参数或URL地址&#xff0c;在后台处理时会发生转换错误。在有些传递页面使用GB2312&#xff0c;而在接收页面使用UTF8&#xff0c;这样接收到的参数就可能会与原来发生不一致…

RSA非对称加密

现实网络中&#xff0c;web应用的开发少不了需要对敏感信息来进行加密&#xff0c;但是加密又不能草草了事&#xff0c;过于简单。一旦被钻了空子&#xff0c;就成了事故。比如密码。 之前用到了非对称加密没做记录&#xff0c;这次又用到了&#xff0c;所以记录下来&#xff…

ajax请求里面调用window.open会被浏览器拦截

场景&#xff1a; 业务系统需要跳转到其他系统-支付平台去完成支付&#xff0c;使用的 window.open(url)&#xff0c;跳转之前需要做一系列的ajax验证&#xff0c; 那么问题来了&#xff0c;window.open放在ajax校验完成并且成功后才会调用&#xff0c;这时浏览器会觉得这个弹窗…

SHA1WithRSA 数字签名

在做支付业务的时候&#xff0c;需要验证信息的正确性&#xff0c;所以选取用到了数字签名&#xff0c;精简如下&#xff1a; 私钥签名&#xff0c;公钥验证签名. 业务平台(信息传送端)&#xff1a;私钥 用于对传送数据进行签名(sign) 支付平台(信息接收端)&#xff1a;公…

can not find a temporary directory:internal error

早上突然发现svn无法更新和提交代码&#xff0c;提示以下错误信息&#xff1a; 因为之前都是好的&#xff0c;突然出现的这个问题&#xff0c;怀疑是以下几点的问题&#xff1a; 1. 磁盘满了 df -lh 看了下确实磁盘满&#xff0c;删除一些不必要的文件就可以了 [df命令的…

图片验证码

前台页面&#xff1a; <li><span><em>*</em>验证码&#xff1a;</span><input type"text" class"input-txt yzm-w" name"imgCode" placeholder"请输入图片验证码"/><img src"/user/getVe…

intellij maven 创建新module

一、 二、 三、 四、目录结构&#xff1a;