I have a page which is created using bootstrap CSS which has a button on it like this one.
<div class="col-sm-4 m-b-xs">
<a data-title="New customer" class="btn btn-primary btn-outline btn-sm modalLink" href="/Registry/CustomerCreatePartial">
<i class="fa fa-plus"></i> Add customer
</a>
</div>
When the user click on the button jQuery handler is executed which in turn calls another javascript function
(function ($) {
$('a.modalLink').on('click', function (event) {
event.preventDefault();
var url = $(this).attr('href');
var title = $(this).attr('data-title');
CreateModalDialog(url, title, 'Cancel', 'Save');
});
})(jQuery);
The code is like the following:
function CreateModalDialog(url, title, cancelText, submitText) {
var modal = $('#myModal');
modal.on('show.bs.modal', function (event) {
var theModal = $(this);
// Get the form from the server.
$.get(url, function (data) {
//Add the form to the modal body
theModal.find('.modal-body').html(data);
});
//wire up form validation
theModal.find('form').validate();
//set up form submission
var okButton = theModal.find('.modal-footer button.buttonSave');
okButton.on('click', function (event) {
//submit the form
theModal.find('form').submit();
});
});
// wire up the actual modal functionality and show the dialog
modal.modal({
"backdrop": "static",
"keyboard": true,
"show": true
});
}
Unfortunately the validation does not occurs and the form get submitted.
My controls on the form uses data-val-*
attribute validation like in the following example.
<input type="text" value="" name="Zip" id="Zip"
data-val-required="Field required"
data-val-length-min="5" data-val-length-max="5"
data-val-length="Zip field should be 5 characters long"
data-val="true" class="form-control">
Where do I am wrong?
EDIT:
I am not using jQuery Unobtrusive validation at all. Just pure jQuery Validate plugin. Just noted that the default HTML extensions generates markup like the one I have added above with attributes like data-val-*
and data-msg-*
. I have aldo noted that jQuery Validation search for data-rule-*
and data-msg-*
. It could be this one the cause of the problems I am experiencing?