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

查看:104
本文介绍了需要一种方法来能够使用元素来验证div(使用不引人注目的/jquery验证插件),而无需查看任何形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近已被分配到一个MVC3项目,在该项目中,我需要在其所有页面上添加客户端验证.

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

不幸的是,这个项目有很多页面,在给定视图的整个页面层次结构中(从布局到最深的局部视图),页面上没有任何形式,尽管我知道这是错误的,并且jquery验证和非侵入性都需要字段要验证是否可以保存在表单中,
但是我的双手一直束缚在这里,因为该项目自最近两年以来一直在运行项目,现在已经进入验证支持部分,因此现在没有高级技术涉众(经理和架构师)准备向所有缺少它的页面添加表单,

所以问题来了: 我可以在没有表单的情况下验证视图或视图的一部分(只是div)吗?如果是,即使有解决方法,怎么办?.请分享您的想法.

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.

我在物质搜索中的详细信息:
我在SO中搜索了相当不错的时间,发现很少有链接提出可以做到的建议,这样的链接是: 没有"form"形式的jQuery验证;标签

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

根据我能看到的所有建议,我创建了2个小提琴:
一种形式- http://jsfiddle.net/here4fiddle/r2w2u/4/(验证并可以看到错误)
其他没有它. http://jsfiddle.net/here4fiddle/r2w2u/5/

在这里,这第二个小提琴有3种方法(1.1和1.2几乎相同,还有2种),但是在未执行和运行时都不会显示验证失败,都说验证通过(isValid = true)尽管输入字段为空白(在第二小提琴中请注意,页面上没有表单,因为我想解决无表单情况的解决方案).

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.



第二个小提琴的代码:
html:

显示错误
javacsript:

函数validate(e){ var isValid = true;



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);
});


推荐答案

引用OP:

需要一种方法来使用元素来验证div(使用不引人注目的/jquery验证插件)而无需在视图上显示任何形式"

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

"我可以在没有form的情况下验证视图或视图的一部分(只是div)吗?如果可以,即使有解决方法,该怎么办?"

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

否.绝对不会.

为什么?因为 jQuery Validate插件旨在用于<form></form>容器内的输入元素.如果<form>不存在,则无法初始化Validation插件.

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.

此外,AFAIK,如果没有jQuery Validation插件,则不能使用Unobtrusive Validation插件.

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

没有解决方法,除非您使用jQuery Validate插件以外的其他东西.

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

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

这篇关于需要一种方法来能够使用元素来验证div(使用不引人注目的/jquery验证插件),而无需查看任何形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆