An approach is needed to be able to validate divs with elements (using the unobtrusive / jquery validation module) without having any form in the view

I was recently assigned to the MVC3 project, where I need to add client-side validation to all of its pages.

Unfortunately, this project has many pages that on it do not have any form in the hierarchy of the entire page of this view (from the layout to the deepest partial view), although I know that this is wrong, and that jquery validation and so unobtrusive both fields are necessary for confirmation in the form,




















ready to add forms to all missing pages,

So the question arises: Is it possible to check the view or part of it (just a div) without a form? if so, even if with workarounds, then how? . please share your thoughts.

Details of my material search:
I was looking for a pretty good time in SO and found some links suggesting that this could be done, one of those links: checking jQuery without the "form" tag

Based on all the suggestions that I could see, all from SO, I created 2 violins:
one of which has the form - http://jsfiddle.net/here4fiddle/r2w2u/4/ (checks and can see the error)
others without him. http://jsfiddle.net/here4fiddle/r2w2u/5/

here this second violin has 3 approaches (1.1 and 1.2 are almost the same and 2), but none of them, when it is working and not running, will show that the checks failed, everyone says that the check passed (isValid = true) although the input field is empty (there is no form on the page in the second script, since I want a solution without scripts ).

Please fix any of the approaches in the second fiddle (if it can work with changes) else, if you have another suggestion that might work, please share.



code for the second violin:
html:

Show error
javacsript:

function validate (e) {var isValid = true;

var $divToCheck = $("#divToCheck"); //====================================== //aproach-1.1 /* var abc = $("<form>").append($divToCheck).validate(); alert(abc); alert(abc.valid()); alert(abc.errorList.length); */ //====================================== //approach-1.2 - start isValid = $("<form>").append($divToCheck).valid(); alert(isValid); //approach-1.2 - end //====================================== //approach 2 - start /* jQuery('#divToCheck').wrap('<form id="temp_form_id" />'); isValid = jQuery('#temp_form_id').valid(); jQuery('#divToCheck').unwrap(); if (isValid) { alert('no errors'); } else{ alert('error'); } */ //approach 2 - end //====================================== } $(document).ready(function () { $("#validate").click(validate); }); 


+1
source share
1 answer

Quote OP:

"An approach is needed to be able to validate divs with elements ( using the unobtrusive / jquery validation module) without any form in the view"

and

" Can I check a view or part of it (just a div ) without form ? If so, even if with workarounds, how?"

No. Absolutely not.

Why? Since the jQuery Validate plugin was designed for use on input elements in the <form></form> container. If <form> does not exist, the Validation plugin cannot be initialized.

Also, AFAIK, you cannot use the Unobruusive Validation plugin without the jQuery validation plugin.

There are no workarounds unless you use something other than the jQuery Validate plugin.

See also: fooobar.com/questions/684345 / ...

+3
source

Source: https://habr.com/ru/post/1272277/


All Articles