Asp.net mvc4 jquery validate dialog box prompt

Source: Internet
Author: User

Recently, asp.net mvc4 was used. There is a need to implement the jquery validate verification message prompt in the pop-up box. The default jquery. validate. unobtrusive. js implementation does not have such a function. You only need to modify it to meet our needs.
Add onDialog:
Function onDialog (errmap, errorList ){
Var messages = "";
$. Each (errorList, function (index, value ){
 
Messages + = value. message;
});
// For testing
If (messages! = ""){
Alert (messages );}
}
Modify 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;
}
Modify $ 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
/// Validation to the form. If parsing just this single element, you shoshould specify true.
/// If parsing several elements, you shoshould 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 ();
}
});
}
};
Call method:
Var qform =$ ("# myForm ");
// Default mode
// $. Validator. unobtrusive. parse (qform );
// Pop-up mode
$. Validator. unobtrusive. parseDialog (qform );
If (qform. valid ()){
// Submit}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.