jquery验证插件validate自定义扩展
<script src="${pageContext.request.contextPath}/resources/js/jquery-1.12.0.min.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/resources/js/jquery.validate.min.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/resources/js/messages_zh.js" type="text/javascript"></script>
<form class="am-form" id="mechantForm" class="doc-vld-msg">
<div class="am-form-group">
<label for="doc-ipt-name-1" class="labelname">厂商名称:</label>
<input type="text" id="merchantName" name="merchantName" minlength="3" placeholder="输入厂商名称" required/>
</div>
<div class="am-form-group">
<label for="doc-ipt-name-1" class="labelname">姓名:</label>
<input type="text" name="name" id="name" placeholder="请输入姓名">
</div>
<div class="am-form-group">
<label for="doc-ipt-phone-1" class="labelname">电话:</label>
<input type="text" name="telphone" id="telphone" placeholder="请输入电话">
</div>
<div class="am-form-group">
<label for="doc-ipt-phone-1" class="labelname">公司地址:</label>
<input type="text" name="address" id="address" placeholder="请输入公司地址">
</div>
<div style="margin-left:15%;margin-top:27px;">
<button style="margin-right:20px;" type="button" data-am-modal-close class="mybtn">关闭</button>
<button type="submit" class="mybtn" id='saveBtn'>确定</button>
</div>
</form>
js部分:
jQuery.validator.addMethod("isMobile", function(value, element) {
var length = value.length;
var mobile = /^1[34578]\d{9}$/;/*/^1(3|4|5|7|8)\d{9}$/*/
return this.optional(element) || (length == 11 && mobile.test(value));
}, "请正确填写您的手机号码");
$("#saveBtn").click(function() {
$("#mechantForm").validate({
submitHandler: function(form){
$.ajax({
cache : true,
type : "POST",
url : "merchant",
data : $('#mechantForm').serialize(),
async : false,
error : function(data) {
layer.alert('系统错误', {icon: 0});
},
success : function(data) {
layer.alert('保存成功', {icon: 1});
$('#mechantForm')[0].reset();
}
});
},
rules: {
merchantName: {
required: true,
minlength: 3
},
name:{
required: true,
minlength: 2
},
telphone:{
required: true,
minlength: 11,
maxlength:11,
digits:true,
number:true,
isMobile : true
},
address:{
required: true,
maxlength:50
}
},
messages: {
merchantName: {
required: "不能为空",
minlength: "不能少于3个字符"
},
name:{
required: "不能为空",
minlength: "不能少于2个字符"
},
telphone:{
required: "不能为空",
minlength: "必须11位数字",
maxlength:"必须11位数字",
digits:"必须是数字" ,
number:"请输入有效数字",
isMobile : "手机号格式错误"
},
address:{
required:'不能为空',
maxlength:"不能多于50个字符"
}
}
});
});