如何使用jQuery Validate插件验证整个页面? [英] How can I validate an entire page with the jQuery Validate plugin?

查看:99
本文介绍了如何使用jQuery Validate插件验证整个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,例如在官方文档中,使用jQuery validate插件很容易,请在此处:

Using the jQuery validate plugin is easy in certain situations, such as this, from the official docs, where are here:

<form class="cmxform" id="commentForm" method="get" action="">
  <fieldset>
    <legend>Please provide your name, email address (won't be published) and a comment</legend>
    <p>
      <label for="cname">Name (required, at least 2 characters)</label>
      <input id="cname" name="name" minlength="2" type="text" required>
    </p>
    <p>
      <label for="cemail">E-Mail (required)</label>
      <input id="cemail" type="email" name="email" required>
    </p>
    <p>
      <label for="curl">URL (optional)</label>
      <input id="curl" type="url" name="url">
    </p>
    <p>
      <label for="ccomment">Your comment (required)</label>
      <textarea id="ccomment" name="comment" required></textarea>
    </p>
    <p>
      <input class="submit" type="submit" value="Submit">
    </p>
  </fieldset>
</form>
<script>
$("#commentForm").validate();
</script>

但是,如果您本身没有表单",但是想对整个页面进行验证怎么办?

But what if you don't have a "form" per se, but want to apply validation to your entire page?

我尝试过:

$(window).load(function () {
    . . .
    this.validate();
});

...但是它似乎不起作用.相关问题此处.

...but it doesn't seem to work. Related question here.

推荐答案

您需要验证表单.因此,您可以使用:

You need to validate the form. So you can validate using :

$('#commentForm').validate();

OR

$('#commentForm').valid();

在提交按钮上单击事件

这篇关于如何使用jQuery Validate插件验证整个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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