简体   繁体   English

需要一种能够使用元素来验证div的方法(使用不引人注目的/ jquery验证插件)而无需查看任何形式

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

I have been assigned to a MVC3 project recently where i need to add client side validation to all of it's pages. 最近,我已被分配到一个MVC3项目,在该项目中,我需要向其所有页面添加客户端验证。

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, 不幸的是,这个项目有很多页面,在给定视图(从布局到最深的局部视图)的整个页面层次结构中,没有任何形式的页面,尽管我知道这是错误的,并且jquery验证和如此简洁的方法都需要验证字段以保持在表格内
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? 所以这里出现一个问题: 我可以在没有表单的情况下验证视图或视图的一部分(只是div)吗? 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 我在SO中搜索了相当不错的时间,发现很少有建议说可以做到的链接,这样的链接是: 没有“ form”标签的jQuery验证

based on all suggestion i could see, all from SO, i have created 2 fiddles: 根据所有我可以看到的建议,我创建了2个小提琴:
one having form - http://jsfiddle.net/here4fiddle/r2w2u/4/ (validates and can see error) 一种形式-http://jsfiddle.net/here4fiddle/r2w2u/4/ (验证并可以看到错误)
other without it. 其他没有它。 http://jsfiddle.net/here4fiddle/r2w2u/5/ 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 ). 在这里,这第二个小提琴有3种方法(1.1和1.2几乎相同,还有2种),但是在未执行和运行时都不会显示验证失败,尽管输入字段为空白,但都说验证通过了(isValid = true)注意在第二个小提琴中,页面上没有表单,因为我想要无表单方案的解决方案

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: 的HTML:

Show error 显示错误
javacsript: javacsript:

function validate(e) { var isValid = true; 函数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);
});


Quote OP: 报价OP:

"Need approach to be able to validate div with elements ( using unobtrusive/jquery validation plugin) without having any form on view" “需要一种方法来使用元素来验证div( 使用不引人注目的/ jquery验证插件),而无需查看任何形式

and

" Can I validate a view or part of it (just a div ) without having form ? If yes, even if with workarounds, then how?" 我可以在没有form情况下验证视图或视图的一部分(只是div吗?如果是的话,即使有解决方法,那又如何呢?”

No. Absolutely not. 不,绝对不会。

Why? 为什么? Because the jQuery Validate plugin was designed to be used on input elements within a <form></form> container. 因为jQuery Validate插件旨在用于<form></form>容器内的输入元素。 If the <form> does not exist, the Validation plugin cannot be initialized. 如果<form>不存在,则无法初始化Validation插件。

Additionally, AFAIK, you cannot use the Unobtrusive Validation plugin without the jQuery Validation plugin. 此外,在AFAIK中,如果没有jQuery Validation插件,则不能使用Unobtrusive Validation插件。

There are no workarounds, unless you use something other than the jQuery Validate plugin. 除非您使用的不是jQuery Validate插件,否则没有其他解决方法。

Also see: https://stackoverflow.com/a/15231887/594235 另请参阅: https : //stackoverflow.com/a/15231887/594235

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM