jQuery验证器插件,不使用表单[重复]

时间:2022-11-28 14:30:38

This question already has an answer here:

这个问题在这里已有答案:

I have multiple section is asp.net that submits data in chunk and i want to use jquery validation plugin, but issue is that asp.net wraps everything in form and child forms not wokring right and technically incorrect.

我有多个部分是asp.net,它提交块中的数据,我想使用jquery验证插件,但问题是asp.net包装所有形式和子表单不正确和技术上不正确。

So only alternative is forget about form and implement validation for divs. But all sames i see are using form. As not being not good at jquery i can't figure out how to use this validator on section of page(On div).

因此,唯一的选择是忘记表单并实现div的验证。但我看到的所有相同的都是使用表格。由于不擅长jquery,我无法弄清楚如何在页面部分(On div)上使用此验证器。

Is it possible? or any other good alternative?

可能吗?或任何其他好的选择?

Source: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

3 个解决方案

#1


Like nightingale2k1 said, you should be able to use that plug-in just fine with a FORM. Here's a quick example that uses a DIV instead:

就像nightingale2k1所说的那样,你应该能够使用这个插件就好了。这是一个使用DIV的快速示例:

<div id="pseudoForm">
  <input type="text" name="first_name"/>
  <input type="text" name="last_name"/>
</div>
<script type="text/javascript">
  $("#pseudoForm").validate({
    onfocusout:true,
    rules:{
      first_name:"required",
      last_name:"required"
    });
</script>

Notice how I used "onfocusout:true", which will make the plug-in validate when the user de-selects either element. You'll need to either use something like that, or else hook up your own event (probably in response to a button press) for the validation to be triggered, as the normal trigger (onSubmit) isn't applicable to DIVs.

请注意我是如何使用“onfocusout:true”的,这将使插件在用户取消选择任一元素时进行验证。您需要使用类似的东西,或者连接您自己的事件(可能是为了响应按下按钮)以触发验证,因为正常触发器(onSubmit)不适用于DIV。

#2


I am using bassistance jquery plugin that you mentioned above, and it doesnt require form submmission to do validation. it just validated after "on blur" event triggered.

我正在使用您在上面提到的bassistance jquery插件,并且它不需要表单submmission来进行验证。它只是在“on blur”事件触发后验证。

or if you want to validate it manually you can call like : $("#commentForm").validate(); (read on this doc page [http://docs.jquery.com/Plugins/Validation][1])

或者如果你想手动验证它,你可以调用如下:$(“#commentForm”)。validate(); (请阅读此文档页面[http://docs.jquery.com/Plugins/Validation][1])

#3


In one of my pages I have a div that is sumitted back via Java and Ajax. Prior to submitting, I validate the individual fields using .validate().element( "#myelement" ); I do it all in javascript though, and don't rely at all on the built in automagic asp controls.

在我的一个页面中,我有一个div,通过Java和Ajax进行总结。在提交之前,我使用.validate()。element(“#myelement”);验证各个字段。我在javascript中完成所有操作,并且完全不依赖于内置的自动asp控件。

I hate to say it, but that global form issue is your real problem. It's probably not an option, but if it is, look as switching to asp.net MVC.

我讨厌这样说,但那个全局形式问题是你真正的问题。它可能不是一个选项,但如果是的话,看起来就像切换到asp.net MVC。

#1


Like nightingale2k1 said, you should be able to use that plug-in just fine with a FORM. Here's a quick example that uses a DIV instead:

就像nightingale2k1所说的那样,你应该能够使用这个插件就好了。这是一个使用DIV的快速示例:

<div id="pseudoForm">
  <input type="text" name="first_name"/>
  <input type="text" name="last_name"/>
</div>
<script type="text/javascript">
  $("#pseudoForm").validate({
    onfocusout:true,
    rules:{
      first_name:"required",
      last_name:"required"
    });
</script>

Notice how I used "onfocusout:true", which will make the plug-in validate when the user de-selects either element. You'll need to either use something like that, or else hook up your own event (probably in response to a button press) for the validation to be triggered, as the normal trigger (onSubmit) isn't applicable to DIVs.

请注意我是如何使用“onfocusout:true”的,这将使插件在用户取消选择任一元素时进行验证。您需要使用类似的东西,或者连接您自己的事件(可能是为了响应按下按钮)以触发验证,因为正常触发器(onSubmit)不适用于DIV。

#2


I am using bassistance jquery plugin that you mentioned above, and it doesnt require form submmission to do validation. it just validated after "on blur" event triggered.

我正在使用您在上面提到的bassistance jquery插件,并且它不需要表单submmission来进行验证。它只是在“on blur”事件触发后验证。

or if you want to validate it manually you can call like : $("#commentForm").validate(); (read on this doc page [http://docs.jquery.com/Plugins/Validation][1])

或者如果你想手动验证它,你可以调用如下:$(“#commentForm”)。validate(); (请阅读此文档页面[http://docs.jquery.com/Plugins/Validation][1])

#3


In one of my pages I have a div that is sumitted back via Java and Ajax. Prior to submitting, I validate the individual fields using .validate().element( "#myelement" ); I do it all in javascript though, and don't rely at all on the built in automagic asp controls.

在我的一个页面中,我有一个div,通过Java和Ajax进行总结。在提交之前,我使用.validate()。element(“#myelement”);验证各个字段。我在javascript中完成所有操作,并且完全不依赖于内置的自动asp控件。

I hate to say it, but that global form issue is your real problem. It's probably not an option, but if it is, look as switching to asp.net MVC.

我讨厌这样说,但那个全局形式问题是你真正的问题。它可能不是一个选项,但如果是的话,看起来就像切换到asp.net MVC。