Created
September 11, 2018 12:35
-
-
Save vlapenkov/4bfdbe57e13cae1db241bd43148b3eff to your computer and use it in GitHub Desktop.
Ajax client form validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* usage | |
* ключи обязательно должны быть id или name полей | |
если не указаны - отображается только ValidationSummary | |
showErrors($form, | |
{ | |
"Email": "bad email", | |
"CompanyName": "bad company name" | |
} | |
); | |
*/ | |
function showErrors($form, errorList) { // 'this' is the form element | |
var container = $form.find("[data-valmsg-summary=true]"), | |
list = container.find("ul"); | |
if (!errorList) return; | |
var $validator = $form.validate(); | |
try { | |
$validator.showErrors(errorList); | |
if (list && list.length && $validator.errorList.length) { | |
list.empty(); | |
container.addClass("validation-summary-errors").removeClass("validation-summary-valid"); | |
// $("<li />").html(errorText).appendTo(list); | |
$.each($validator.errorList, function () { | |
$("<li />").html(this.message).appendTo(list); | |
}); | |
} | |
} | |
catch (e) | |
{ | |
var errorText = Object.values(errorList)[0]; | |
list.empty(); | |
container.addClass("validation-summary-errors").removeClass("validation-summary-valid"); | |
$("<li />").html(errorText).appendTo(list); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment