Need approach to be able to validate div with elements(using unobtrusive/jquery validation plugin) without having any form on view
Asked Answered
I

1

2

I have been assigned to a MVC3 project recently where i need to add client side validation to all of it's pages.

unfortunately this project has many pages which don't have any form on it in entire page hierarchy of a given view(from layout till deepest partial view), though i know it's wrong and that jquery validation and so unobtrusive both need fields to validated to be kept inside form,
but my hands are tied here as this project is running project since last 2 years and now entering into validation support part, and so none of senior technical stakeholders(managers and architects) are NOW ready to add forms to all pages missing it,

so here comes the question: Can i validate a view or part of it (just a div) without having form? if yes, even if with workarounds, then how?. please share your thoughts.

Details on my search on matter:
I have searched for quite good time in SO and found few links having suggestion made saying it can be done, one such link is: jQuery validation without "form" tag

based on all suggestion i could see, all from SO, i have created 2 fiddles:
one having form - http://jsfiddle.net/here4fiddle/r2w2u/4/ (validates and can see error)
other without it. http://jsfiddle.net/here4fiddle/r2w2u/5/

here this second fiddle, has 3 approaches(1.1 & 1.2 almost same, and 2), but none of those when uncomented and run, will show that validations fails, all say validation passed (isValid=true) though input field being blank(note in second fiddle there is no form on page as i want solution for without form scenarios).

Please correct any of approach in second fiddle(if it can work with changes) else if have some other suggestion which may work, please share.



code for second fiddle:
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);
});


Infatuated answered 26/5, 2014 at 13:21 Comment(5)
What do you mean when you say "validate"? what is the action before validating? you want to submit the div value to a php file?Tripe
Requesting if someone can edit post so that html pasted just below "code for second fiddle: html" text in post shows up, i am finding it tough to display html code part, meanwhile i will try to learn it through meta site readings. thanks.Infatuated
@Tripe : yes i want to submit div's content to a server side code via jquery.ajax, but before that i need to make sure input field has data filled in it, regarding: "validate", that i did just to make a reference to JQUERY.validator object, so that when isvalid comes false(post validation), then i can access/show errors referring it.Infatuated
Duplicate of: https://mcmap.net/q/820062/-jquery-validation-without-form-tagTwannatwattle
You are mistaken. There is absolutely nothing within #6283180 that says it can be done. In fact, the accepted answer indicates the opposite.Twannatwattle
T
4

Quote OP:

"Need approach to be able to validate div with elements (using unobtrusive/jquery validation plugin) without having any form on view"

and

"Can I validate a view or part of it (just a div) without having form? If yes, even if with workarounds, then how?"

No. Absolutely not.

Why? Because the jQuery Validate plugin was designed to be used on input elements within a <form></form> container. If the <form> does not exist, the Validation plugin cannot be initialized.

Additionally, AFAIK, you cannot use the Unobtrusive Validation plugin without the jQuery Validation plugin.

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

Also see: https://mcmap.net/q/820062/-jquery-validation-without-form-tag

Twannatwattle answered 26/5, 2014 at 14:28 Comment(4)
thanks a lot for your inputs, this is what i was looking for, a firm NO for "can validation work without form?", and regarding the shared link from SO, i already had a look of that too, but after looking at that when i found this: #6283180, i again got to think 'is there a workaround?', do you have any inputs on last post/answer on this SO link, if so then please reply, that would give final touch to any hope i have about existence of any workaround for this. thanks again for your clear inputs.Infatuated
@LearningNeverEnds, look at my profile regarding how many questions tagged with jQuery Validate that I've answered. The plugin requires form tags... there's no possible way around that. The second answer is just trying to dynamically add the form tags. Like he says in that answer... "this should imply that we're going about something wrongly"Twannatwattle
Thanks Sparky. So with this and per your previous answers for validation, i can now conclude that it's not possible to validate page without form, till we are using jquery validate plugin. added so future visitors know what was conclusion for OP.Infatuated
oh i am really sorry, i got busy thinking to convince people that we must get form added to page, if we want validation. that is why delay in accepting the answer, i have accepted it now. thanks again. :-) take care.Infatuated

© 2022 - 2024 — McMap. All rights reserved.