asp.net mvc4 jquery validate 弹出框提示
程序员文章站
2022-06-26 17:47:11
最近的项目采用了asp.net mvc4。有个需求,需要实现jquery validate的验证消息提示采用弹出框方式。默认的jquery.validate.unobtrusive...
最近的项目采用了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) { // Compare against undefined, because an empty message is legal (and falsy)
prefix += "-";
$.each(this.params, function () {
paramValues[this] = $element.attr(prefix + this);
});
this.adapt({
element: element,
form: form,
message: message,
params: paramValues,
rules: rules,
messages: messages
});
}
});
jQuery.extend(rules, { "__dummy__": true });
if (!skipAttach) {
valInfo.attachValidation();
}
},
parse: function (selector) {
/// <summary>
/// Parses all the HTML elements in the specified selector. It looks for input elements decorated
/// with the [data-val=true] attribute value and enables validation according to the data-val-*
/// attribute values.
/// </summary>
/// <param name="selector" type="String">Any valid jQuery selector.</param>
$(selector).find(":input[data-val=true]").each(function () {
$jQval.unobtrusive.parseElement(this, true, false);
});
$("form").each(function () {
var info = validationInfo(this, false);
if (info) {
info.attachValidation();
}
});
},
parseDialog: function (selector) {
/// <summary>
/// Parses all the HTML elements in the specified selector. It looks for input elements decorated
/// with the [data-val=true] attribute value and enables validation according to the data-val-*
/// attribute values.
/// </summary>
/// <param name="selector" type="String">Any valid jQuery selector.</param>
$(selector).find(":input[data-val=true]").each(function () {
$jQval.unobtrusive.parseElement(this, true, true);
});
$("form").each(function () {
var info = validationInfo(this, true);
if (info) {
info.attachValidation();
}
});
}
};
调用方式:
var qform = $("#myForm");
//默认方式
//$.validator.unobtrusive.parse(qform);
//弹出方式
$.validator.unobtrusive.parseDialog(qform);
if (qform.valid()) {
//提交 }
添加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) { // Compare against undefined, because an empty message is legal (and falsy)
prefix += "-";
$.each(this.params, function () {
paramValues[this] = $element.attr(prefix + this);
});
this.adapt({
element: element,
form: form,
message: message,
params: paramValues,
rules: rules,
messages: messages
});
}
});
jQuery.extend(rules, { "__dummy__": true });
if (!skipAttach) {
valInfo.attachValidation();
}
},
parse: function (selector) {
/// <summary>
/// Parses all the HTML elements in the specified selector. It looks for input elements decorated
/// with the [data-val=true] attribute value and enables validation according to the data-val-*
/// attribute values.
/// </summary>
/// <param name="selector" type="String">Any valid jQuery selector.</param>
$(selector).find(":input[data-val=true]").each(function () {
$jQval.unobtrusive.parseElement(this, true, false);
});
$("form").each(function () {
var info = validationInfo(this, false);
if (info) {
info.attachValidation();
}
});
},
parseDialog: function (selector) {
/// <summary>
/// Parses all the HTML elements in the specified selector. It looks for input elements decorated
/// with the [data-val=true] attribute value and enables validation according to the data-val-*
/// attribute values.
/// </summary>
/// <param name="selector" type="String">Any valid jQuery selector.</param>
$(selector).find(":input[data-val=true]").each(function () {
$jQval.unobtrusive.parseElement(this, true, true);
});
$("form").each(function () {
var info = validationInfo(this, true);
if (info) {
info.attachValidation();
}
});
}
};
调用方式:
var qform = $("#myForm");
//默认方式
//$.validator.unobtrusive.parse(qform);
//弹出方式
$.validator.unobtrusive.parseDialog(qform);
if (qform.valid()) {
//提交 }
上一篇: 中国“车联网”专利战硝烟四起
下一篇: pyecharts画柱状图
推荐阅读
-
jQuery实现点击文本框弹出热门标签的提示效果
-
jquery右下角弹出提示框示例代码
-
asp.net mvc4 jquery validate 弹出框提示
-
Asp.Net中避免重复提交和弹出提示框的实例代码
-
jquery右下角弹出提示框示例代码
-
基于jquery的弹出提示框始终处于窗口的居中位置(类似于alert弹出框的效果)_jquery
-
jQuery实现点击文本框弹出热门标签的提示效果
-
Jquery实现鼠标移上弹出提示框、移出消失思路及代码_jquery
-
Jquery实现鼠标移上弹出提示框、移出消失思路及代码_jquery
-
基于jquery的弹出提示框始终处于窗口的居中位置(类似于alert弹出框的效果)_jquery