jquery中的validate检测使用方法

首先需要jquery.validate.min.js,于jsp页面中加载

<script type="text/javascript" src="../js/jquery.validate.min.js"></script>


首先用一个函数包装

checkForm = function () {

1,书写自定义检测函数(validate.js里本身也提供一些检测方法)
	//返回false报错,true通过
	$.validator.addMethod("checkName", function(value, element) {
		if(value == ""){
			return false;
		}else return true;
	}, "请选择。");


2,检测部分

<span style="white-space:pre">	</span>$("#editForm").validate({
		ignore: "",//不知道干啥用
		focusInvalid:false,//不知道干啥用
		rules:{//检测规则
			projectName:		        "checkName",//自定义检测
			projectCode:	   	{
				maxlength:				16,//长度
			},
			decisionDateStart:	{
				required:		        true,//不为空
				dateISO:		        true,//日期格式
			},
		},
		messages: {//对应的提示信息
			projectName:		        "名字错误。",
			projectCode:	   	{
				maxlength:				"长度超过",
			},
		},
		errorPlacement: function(error) {//有错的时候
				$("#FormError").append(error);
				//error貌似是信息全部加起来的东西
				var s = $("#FormError").text();
			    s = s.replace(/。/g,"。<br/>");//换行
				jAlert(s,'WARNING');//警告
			},
		onclick:                        false,
		onfocusout:                     false,
		onkeyup:                        false,
		submitHandler: function(editForm) {//没错的时候
			$(":button").attr({"disabled":"disabled"});//让所有按钮失效
			editForm.submit();
		},
	});
		$.alerts.dialogClass = "style_1";
		$("#FormError").empty();
};





你可能感兴趣的:(jquery中的validate检测使用方法)