asp.net mvc4 jquery validate 弹出框提示
最近的项目采用了asp.net mvc4。有个需求,需要实现jquery validate的验证消息提示采用弹出框方式。默认的jquery.validate.unobtrusive.js实现中没有这样的功能,只要稍加修改就可以满足我们的需求。添加onDialog:
function onDialog(errmap, errorList) {
var messages = "";
$.each(errorList, function (index, value) {
messages += value.message;
});
//测试用
if(messages!=""){
alert(messages); }
}
修改validattionInfo:
function validationInfo(form, dialog) {
var $form = $(form),
result = $form.data(data_validation);
if (!result) {
result = {
options: { // options structure passed to jQuery Validate's validate() method
errorClass: "input-validation-error",
errorElement: "span",
errorPlacement: $.proxy(onError, form),
invalidHandler: $.proxy(onErrors, form),
messages: {},
rules: {},
success: $.proxy(onSuccess, form)
},
attachValidation: function () {
$form.validate(this.options);
},
validate: function () { // a validation function that is called by unobtrusive Ajax
$form.validate();
return $form.valid();
}
};
if (dialog) {
result.options.showErrors = $.proxy(onDialog, form);
result.options.onfocusout = false;
reuslt.options.onkeyup = false;
}
$form.data(data_validation, result);
}
return result;
}
修改$jQval.unobjtrusive:
$jQval.unobtrusive = {
adapters: [],
parseElement: function (element, skipAttach, dialog) {
/// <summary>
/// Parses a single HTML element for unobtrusive validation attributes.
/// </summary>
/// <param name="element" domElement="true">The HTML element to be parsed.</param>
/// <param name="skipAttach" type="Boolean">[Optional] true to skip attaching the
/// validation to the form. If parsing just this single element, you should specify true.
/// If parsing several elements, you should specify false, and manually attach the validation
/// to the form when you are finished. The default is false.</param>
var $element = $(element),
form = $element.parents("form")[0],
valInfo, rules, messages;
if (!form) { // Cannot do client-side validation without a form
return;
}
valInfo = validationInfo(form, dialog);
valInfo.options.rules[element.name] = rules = {};
valInfo.options.messages[element.name] = messages = {};
$.each(this.adapters, function () {
var prefix = "data-val-" + this.name,
message = $element.attr(prefix),
paramValues = {};
if (message !== undefined)
补充:Web开发 , ASP.Net ,