I use
asp.net mvc 3 jquery validate unobstructive javascript.
I am trying to write all my validation on the server via annotations and then add a new mvc 3 function to take care of the client side.
I have a dialog on which there is a button (just a button is not a submit button) that I want to send data to the server through ajax.
So, when the user clicks on the button, I make the submit form and return false to cancel the response.
I thought this would lead to a check, but that doesn't seem to be the case. How to make a client-side validation trigger?
Edit
<form method="post" id="TaskFrm" action="/Controller/Action"> <input type="text" value="" name="Name" id="Name" data-val-required="Name field cannot be left blank" data-val-length-max="100" data-val-length="task cannot exceed 100 characters" data-val="true"> </form> var $dialog = $('<div></div>').dialog( { width: 580, height: 410, resizable: false, modal: true, autoOpen: false, title: 'Basic Dialog', buttons: { Cancel: function () { $(this).dialog('close'); }, 'Create Task': function () { var createSubmitFrmHandler = $(my.selectors.createFrm).live('submit', function () { alert('hi'); return false; }); createSubmitFrmHandler .validate(); var a = createSubmitFrmHandler .valid(); alert(a); } } });
This always returns true.
Edit 2
if I put a submit button on the form, it will show client validation (I use jquery to return false, as shown in my code).
So this means that I have scripts and that’s it, but it doesn’t work for unknown reasons when I try to make it programmatic.
Edit 3
I have inserted jquery validate && & jquery.validate.unobtrusive files on the main page. But when I stick to them in a partial view that contains fields that are loaded, and then forcibly submits confirmations.
I do not understand. I'm pretty sure the path is right, as I just dragged and dropping the file and launched it on my main page, and he understood the path. Having it in partial views is not a solution, since I'm going to do it several times, which means that every time a partial view is loaded, I get another copy of these files.
Change 4
I think this is just jquery.validate.unobtrusive, which for some reason needs to be loaded every time. However, I do not know why.