扩展jquery手动添加MVC的Model验证格式的错误信息
jQuery.extend(jQuery.validator.messages, {
required: "此项必填。",
remote: "请修正此项。",
email: "请输入一个合法的电子邮件地址。",
url: "请输入合法网址。",
date: "请输入合法日期。",
dateISO: "请输入合法日期(ISO格式)。",
number: "请输入合法数字。",
digits: "请只输入数字。",
creditcard: "请输入一个有效的信用卡号。",
equalTo: "请再次输入相同的值。",
maxlength: jQuery.validator.format("请输入不超过{0}个字符。"),
minlength: jQuery.validator.format("请输入至少{0}个字符。"),
rangelength: jQuery.validator.format("请输入介于{0}和{1}个长的字符值。"),
range: jQuery.validator.format("请输入介于{0}和{1}的值。"),
max: jQuery.validator.format("请输入一个小于或等于{0}的值。"),
min: jQuery.validator.format("请输入一个大于或等于{0}的值。")
});
//光标移出时触发客户端验证
$.validator.setDefaults({
//光标移出时
onfocusout: function (element) {
this.element(element);
},
//光标移入时
onfocusin: function (element, event) {
//找到显示错误提示的标签并移除,针对jquery.validate.unobtrusive
var errorElement = $(element).next('span.field-validation-error');
if (errorElement) {
errorElement.children().remove();
}
},
onkeyup: function (element, event) {
}
});
//添加Model验证错误信息
$.extend(
{
appendValidateMessage: function (errorList) {
if (errorList == null || errorList == 'undefined' || errorList.length == 0) {
return;
}
for (var i = 0; i < errorList.length; i++) {
$('#' + errorList[i].Key)
.after(
'<span class="text-danger field-validation-error" data-valmsg-for="' +
errorList[i].Key +
'" data-valmsg-replace="true"><span for="' +
errorList[i].Key +
'" class="">' +
errorList[i].Value +
'</span></span>');
}
},
clearValidation: function () {
$('.text-danger.field-validation-error').remove();
}
});
////清除验证信息
//jQuery.fn.clearValidation = function () {
// var $this = $(this);
// var v = $this.validate();
// //var v = formToClearValidation.validate();
// $('[name]', $this).each(function () {
// v.successList.push(this);
// v.showErrors();
// });
// v.resetForm();
// v.reset();
//};
jQuery.fn.extend({
valid: function () {
var err = $(this).find('.text-danger.field-validation-error').find('span[for]');
if (err.length > 0) {
return false;
}
return true;
}
});
$.validator.addMethod('qeCartonNo', function (value, element) {
//QE箱號验证规则
return this.optional(element) || /^[A-Z]{2,}-[A-Z]{2,}\d{1,}-[A-Z]{1,}-\d{1,}-[0-9]{4}$/.test(value);
}, 'Carton 编码原则 :客户+机种+阶段+数量+号码,类似:MS-SM3370-EVT-10-0001');
$.validator.addMethod('qeEquipmentNo', function (value, element) {
//QE机台号正则验证规则
return this.optional(element) || /^[A-Z]{2}-Tester-[0-9]{4}$/.test(value);
}, '設備號格式为:英文单词的第一个字母-Tester-XXXX類似:MB-Tester-0001');
=====================
使用:
$("#save").on("click",
function () {
var isValid = form.valid();
if (!isValid) {
return false;
}
form.submit();
});
$('#qeWorkOrderForm').submit(function (e) {
e.preventDefault();
$('#save').attr('disabled', 'disabled');
var formData = new FormData($("form")[0]);
$.ajax({
url: '@Url.Action("Create","QeWorkOrder")',
type: "POST",
data: formData, //form.serialize(),
async: false,
cache: false,
contentType: false,
processData: false,
success: function (result) {
result = new Result(result);
debugger;
try {
if (result.status == 2) {
$.clearValidation();
$.appendValidateMessage(result.modelStateErrList);
warning();
} else {
layer.msg(result.message);
}
} catch (err) {
}
},
error: function (data) {
debugger;
try {
layer.msg("error:" + data.responseText);
} catch (err) {
}
warning();
},
complete: function () {
$('#save').removeAttr('disabled');
$('#loadingDiv').hide();
}
});
});