如何有条件地执行 jquery 验证?

发布于 2024-09-03 08:42:20 字数 996 浏览 3 评论 0原文

我正在使用 jquery 验证插件验证表单......

 rules: {
    Name: "required",
    MobileNo: {
           required: true,
           minlength: 10, remote: '<%=Url.Action("getClientMobNo", "Clients") %>'
               },
    Address: "required"
            },
    messages: {
        Name: "please provide a client name",
        MobileNo: {
        required: "Please provide a mobile phone no",
        rangelength: jQuery.format("Enter at least {0} characters"),
        remote: jQuery.format("This MobileNo is already in use")
      },
      Address: "please provide client address"
   },

这在添加表单验证方面效果很好,但我使用相同的表单进行编辑,他们可以使用相同的手机号,但我的插件验证了 mobileno 说已经有一个mobileno...但是如何根据条件执行远程属性,

   MobileNo: {
           required: true,
           minlength: 10,
          if($("#HfId").val() == ""){ 
            remote: '<%=Url.Action("getClientMobNo", "Clients") %>'
             }
          },

这是一个有效的jquery条件验证语句....如何根据条件跳过远程属性....

I am validating form using jquery validation plugin......

 rules: {
    Name: "required",
    MobileNo: {
           required: true,
           minlength: 10, remote: '<%=Url.Action("getClientMobNo", "Clients") %>'
               },
    Address: "required"
            },
    messages: {
        Name: "please provide a client name",
        MobileNo: {
        required: "Please provide a mobile phone no",
        rangelength: jQuery.format("Enter at least {0} characters"),
        remote: jQuery.format("This MobileNo is already in use")
      },
      Address: "please provide client address"
   },

This works pretty well on add form validation but i use the same form for edit here they can use the same mobile no,but my plugin validates that mobileno saying there is already a mobileno... But how to execute remote attribute based on a condition,

   MobileNo: {
           required: true,
           minlength: 10,
          if($("#HfId").val() == ""){ 
            remote: '<%=Url.Action("getClientMobNo", "Clients") %>'
             }
          },

Is this a valid jquery conditional validation statement.... How to skip remote attribute based on a condition....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回梦 2024-09-10 08:42:20

好吧,我会做这样的事情

,例如我的 php 页面是

要添加的 member.php,发送像这样的 URL member.php?action=add

<? 
$action = $_GET['action'];
?>
<script>
        $("#frmmember").validate({
      rules: { 
        name: { 
                <? if($action=='add') { ?>
                      required: true, 
               <? } ?>
                      rangelength: [4, 50] },
        email: { required: true, rangelength: [5, 50], email: true },
        phone: { required: true, number:true, rangelength: [7, 10] }
        },
      onkeyup: false
    });
</script>

在这种情况下,验证规则 required=true 仅在存在时才适用url 变量中的“add”“action”

Well, I would do something like this

for example my php page is member.php

to add, send a URL like this member.php?action=add

<? 
$action = $_GET['action'];
?>
<script>
        $("#frmmember").validate({
      rules: { 
        name: { 
                <? if($action=='add') { ?>
                      required: true, 
               <? } ?>
                      rangelength: [4, 50] },
        email: { required: true, rangelength: [5, 50], email: true },
        phone: { required: true, number:true, rangelength: [7, 10] }
        },
      onkeyup: false
    });
</script>

In this case, the validation rule required=true is only going to apply if there is "add" in the url variable "action"

半山落雨半山空 2024-09-10 08:42:20

您将需要一些服务器端。处理编辑或应用不同的 Url.Action。

要执行不同的操作来管理它正在编辑详细信息而不是以相同的方式应用它们,因此 mobileno 可以是相同的。

祝你好运。

编辑 是因为服务器端验证而返回,还是 Jquery 在到达那里之前就将其返回?

You will need some server side. Either to handle the edit or to apply a different Url.Action.

To perform a differnet action to manage that it is editing the details not applying them in the same way, thus mobileno can be the same.

Good luck.

Edit Is it throwing back because of server side validation or does Jquery throw it back before it even gets there?

我的鱼塘能养鲲 2024-09-10 08:42:20

您可以在验证规则初始化后强制删除规则,使用 rules( '删除')方法

$('#yourForm').validate({
  rules: {
    Name: "required",
    MobileNo: {
      required: true,
      minlength: 10, remote: '<%=Url.Action("getClientMobNo", "Clients") %>'
    },
    Address: "required"
  },
  messages: {
    Name: "please provide a client name",
    MobileNo: {
      required: "Please provide a mobile phone no",
      rangelength: jQuery.format("Enter at least {0} characters"),
      remote: jQuery.format("This MobileNo is already in use")
    },
    Address: "please provide client address"
  }
});

if ($('#HfId').val() !== '')
  $('#MobileNo').rules('remove', 'remote');

这样,您就不必用内联逻辑弄乱共享的声明性规则。即使添加页面上不存在#HfId,它也会默默地失败而不会出现错误(显然,不会删除远程规则),因此它适用于添加和编辑状态。

You can imperatively remove rules after the validation rules have been initialized, using the rules('remove') method.

$('#yourForm').validate({
  rules: {
    Name: "required",
    MobileNo: {
      required: true,
      minlength: 10, remote: '<%=Url.Action("getClientMobNo", "Clients") %>'
    },
    Address: "required"
  },
  messages: {
    Name: "please provide a client name",
    MobileNo: {
      required: "Please provide a mobile phone no",
      rangelength: jQuery.format("Enter at least {0} characters"),
      remote: jQuery.format("This MobileNo is already in use")
    },
    Address: "please provide client address"
  }
});

if ($('#HfId').val() !== '')
  $('#MobileNo').rules('remove', 'remote');

That way you don't have to clutter up your shared declarative rules with inline logic. Even if #HfId doesn't exist on the add page, that will silently fall through without error (and without removing the remote rule, obviously), so it's applicable to both add and edit states.

鱼忆七猫命九 2024-09-10 08:42:20

我很惊讶没有其他人提到这样做,但我建议使用添加规则方法。创建验证对象后,您可以根据需要向对象动态添加规则。

 if($("#HfId").val() == "") {
      $("#MobileNo").rules("add", {
          remote: '<%=Url.Action("getClientMobNo", "Clients") %>'
      });
 }

这样做的优点是,如果您在插入新记录时要添加多个规则,则可以将它们全部分组到一个位置。如果您的逻辑变得更加复杂,您还可以多次调用 add 。我更喜欢在需要时添加东西,然后调用remove将它们删除。您可以在调用 validate() 后执行此操作。

I'm surprised nobody else has mentioned doing it this way, but I would suggest using the add rules method. After you've created your validation object, you can dynamically add rules to an object as needed.

 if($("#HfId").val() == "") {
      $("#MobileNo").rules("add", {
          remote: '<%=Url.Action("getClientMobNo", "Clients") %>'
      });
 }

The advantage of doing it this way is if you have multiple rules to add when inserting a new record, you can group them all in one place. You can also call add multiple times if your logic becomes more complex. I much prefer adding things when needed, then calling remove to take them away. You do this after calling validate().

内心荒芜 2024-09-10 08:42:20

使用 jquery 扩展方法怎么样?如果是新记录,则添加远程属性;如果是现有记录,则使用空对象。

 rules: {
        Name: "required",
        MobileNo: $.extend(
          {
            required: true,
            minlength: 10
          },
          ($("#HfId").val() == "")?{remote: '<%=Url.Action("getClientMobNo", "Clients") %>'}:{}
        ),
        Address: "required",
        messages: {
            Name: "please provide a client name",
            MobileNo: {
              required: "Please provide a mobile phone no",
              rangelength: "Enter at least {0} characters",
              remote: "This MobileNo is already in use"
            },
          Address: "please provide client address"
       }
     }

What about using the jquery extend method? Add the remote property if it's a new record or use and empty object if it's and exiting record.

 rules: {
        Name: "required",
        MobileNo: $.extend(
          {
            required: true,
            minlength: 10
          },
          ($("#HfId").val() == "")?{remote: '<%=Url.Action("getClientMobNo", "Clients") %>'}:{}
        ),
        Address: "required",
        messages: {
            Name: "please provide a client name",
            MobileNo: {
              required: "Please provide a mobile phone no",
              rangelength: "Enter at least {0} characters",
              remote: "This MobileNo is already in use"
            },
          Address: "please provide client address"
       }
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文