Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Created March 3, 2013 22:40
Show Gist options
  • Save tugberkugurlu/5078653 to your computer and use it in GitHub Desktop.
Save tugberkugurlu/5078653 to your computer and use it in GitHub Desktop.
jQuery Validate for Ajax Requests
// These are jQuery fns.
(function ($) {
$.fn.removeMVCValidationSummary = function (settings) {
var form = $(this),
container = form.find("[data-valmsg-summary=true]"),
list = container.find("ul");
container.removeClass("validation-summary-errors").addClass("validation-summary-valid");
return form;
};
$.fn.raiseMVCUnobtrusiveErrorsManually = function(exceptions) { // 'this' is the form element
var container = $(this).find("[data-valmsg-summary=true]"),
list = container.find("ul");
if (list && list.length) {
list.empty();
container.addClass("validation-summary-errors").removeClass("validation-summary-valid");
$.each(exceptions, function () {
$("<li />").html(this.ErrorMessage).appendTo(list);
});
}
}
$.fn.clearForm = function() {
var formid = $(this).attr("id");
$(':input', '#' + formid).not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
return $(this);
}
})(jQuery)
// This is the actual code for the page
(function () {
$(function () {
$("#apiConsumerRegistrationForm-ajax-success-message").hide();
//setting the first td width of the table.
$(".form-table tr").each(function (index) {
$(this).find('td:first').css('width', '300px').after('<td style="width: 7px;"><strong>:</strong></td>');
});
$("#apiConsumerRegistrationForm").submit(function (e) {
var form = $("#apiConsumerRegistrationForm");
if (form.valid()) {
form.validate();
var action = form.attr("action");
var f = form.serialize();
$("#ajax-progress-dialog").dialog("open");
$.ajax({
type: "POST",
url: action,
data: f,
success: function (r) {
if (r.isSucceeded) {
form.removeMVCValidationSummary();
form.clearForm();
var messageContainer = $("#apiConsumerRegistrationForm-ajax-success-message");
messageContainer.html(
$('<ul></ul>').append($('<li></li>').text(r.successMessage))
).show("slow", function () {
// Animation complete. do what you need here
// maybe call an image here.
$('html, body').animate({scrollTop:messageContainer.offset().top - 40}, 500);
});
//$.showMessage(r.successMessage, {
// position: 'top', delay: 3000, speed: 1000, size: 50, backgroundColor: '#485C7F', fontSize: '20px'
//});
}
},
error: function (req, status, error) {
alert("error : req=" + req + "&status=" + status + "&error=" + error);
},
complete: function () {
$("#ajax-progress-dialog").dialog("close");
}
});
e.preventDefault();
}
});
});
// Checkbox Validation
jQuery.validator.addMethod("checkrequired", function (value, element) {
var checked = false;
checked = $(element).is(':checked');
return checked;
}, '');
jQuery.validator.unobtrusive.adapters.addBool("truerequired", "checkrequired");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment