jQuery validator plugin without using form [duplicate]

人走茶凉 提交于 2019-12-30 09:54:13

问题


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.

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).

Is it possible? or any other good alternative?

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


回答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:

<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.




回答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.

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])




回答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.

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.



来源:https://stackoverflow.com/questions/1046739/jquery-validator-plugin-without-using-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!