验证同一页面上的多个表单 [英] Validating multiple forms on the same page

查看:272
本文介绍了验证同一页面上的多个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我在页面中有3个具有相同名称的表单.如何使用一种验证方法来验证所有这些表格?

For example I have 3 forms in a page with the same names. How can I validate all these forms with one validate method?

<form id="formID">
<input name="nick">
<input type="submit">
</form>

<form id="formID">
<input name="nick">
<input type="submit">
</form>

<form id="formID">
<input name="nick">
<input type="submit">
</form>

我只想验证提交的表单,而不能一次全部验证.

I want to validate only the submitted form not all at the same time.

$('#formID').validate({
})

我的解决方案不起作用.我还发现我无法添加不同的ID,因为这些表单位于PHP循环中,并且必须具有相同的ID.

My solution does not work. I also find I can't add different IDs because these forms are in a PHP loop and it must have same ID.

推荐答案

这是在单个页面上验证多个表单的解决方案:

Here is the Solution to Validate Multiple Forms on a Single Page :

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>

<script type="text/javascript">
  $(document).ready(function () {
    $("form").submit(function () {

      var clikedForm = $(this); // Select Form

      if (clikedForm.find("[name='mobile_no']").val() == '') {
        alert('Enter Valid mobile number');
        return false;
      }
      if (clikedForm.find("[name='email_id']").val() == '') {
        alert('Enter  valid email id');
        return false;
      }

    });
  });
</script>

它正在为我工​​作.我在一个页面中为5种表单编写了这段代码.

It is working for me. I wrote this code for 5 Forms in a single Page.

这篇关于验证同一页面上的多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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