【问题标题】:ClientValidationFunction in Custom validator not firing自定义验证器中的 ClientValidationFunction 未触发
【发布时间】:2014-02-25 05:47:36
【问题描述】:

Google 充满了这类问题,但我想我的问题有点不同。 不同之处在于我在这里使用了masked input 我包括jquery.maskedinput-1.2.2.jsjquery-1.7.2.min.js 来使用masked inputs。屏蔽代码是

 $(document).ready(
    function () {
        $("#txtPhoneNo").mask("?(999) 999-9999");
    });

但它没有验证输入的长度,所以我为此写了javascript 并使用了“CustomValidator”,如下所示。

function isPhoneNumberValid(sender, args) {alert("l");
    if (args.value.length < 2)
        args.isValid = false;
}

alert("l"); 本身会告诉你,我只是想看看这个js function 是否会触发。

textboxCustomValidator 的代码如下。

<asp:TextBox ID="txtPhoneNo" runat="server" CssClass="textbox3" size="60" Width="350px"
     TabIndex="4" ClientIDMode="Static"></asp:TextBox>

     &nbsp;<span style="font-size: 12px;">(000-000-0000)</span>  

<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Phone number is not valid"
      ClientValidationFunction="isPhoneNumberValid" Font-Size="9pt" ForeColor="Red" ControlToValidate="txtPhoneNo"></asp:CustomValidator>

注意

这里在textboxClientIDMode="Static"被使用,这可能是个问题。

问题

问题是我的js function 根本没有触发。如果文本长度小于 2 但不显示,则必须显示警报,而是进行回发。

谁能帮帮我。 谢谢

【问题讨论】:

  • 你错过了文本框中函数isPhoneNumberValid的参数
  • 您是否也注意到了valueisValid 的大小写?
  • 这就是我之前使用alert()的原因
  • 我的意思是在将value 替换为ValueisValid 替换为IsValid 后进行检查

标签: javascript jquery asp.net customvalidator


【解决方案1】:

您的主要问题是,文本框是空的,因此在用户在文本框中键入任何值之前未分配任何参数,因此不会调用该函数。如果您在文本框中键入任何值,则自定义验证器运行良好,因此您需要 RequiredFieldValidator

试试这个而不是你的代码

 <script>
    function isPhoneNumberValid(sender, args) {
        alert("l");
        if (args.Value.length < 2)
            args.isValid = false;
    }
</script>

<h3>We suggest the following:</h3>
<asp:TextBox ID="txtPhoneNo" runat="server" CssClass="textbox3" size="60" 
             Width="350px" TabIndex="4"></asp:TextBox>
&nbsp;<span style="font-size: 12px;">(000-000-0000)</span>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
     ValidationGroup="xxx" ControlToValidate="txtPhoneNo" Font-Size="9pt" 
     Display="Dynamic" ForeColor="Red" ErrorMessage="This field is Empty">
</asp:RequiredFieldValidator>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage=
     "Phone number is not valid" ClientValidationFunction="isPhoneNumberValid" 
     Font-Size="9pt" ForeColor="Red" ValidationGroup="xxx" Display="Dynamic" 
     ControlToValidate="txtPhoneNo">
</asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="xxx" />

您需要先检查 asp:RequiredFieldValidator,然后使用自定义验证器

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
     ValidationGroup="xxx" ControlToValidate="txtPhoneNo" Font-Size="9pt" 
     Display="Dynamic" ForeColor="Red" ErrorMessage="This field is Empty">
</asp:RequiredFieldValidator>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多