【问题标题】:prevent The Client side validation go to server side防止客户端验证转到服务器端
【发布时间】:2016-03-03 01:28:17
【问题描述】:

我有一个表单,我有一个 JavaScript 验证。如果表单无效,如何防止提交按钮转到服务器端?

<button id="LoginButton" onclick="Login.initReuiredValidation();"
        onserverclick="LoginButton_Click" runat="server" type="submit" 
        class="submit btn btn-primary pull-right"> 
    <asp:Literal runat="server" meta:resourcekey="LoginButton" />                                    
    <i class="icon-angle-right"></i>                    
</button>

【问题讨论】:

  • 我会使用带有 ClientValidationFunction 的 CustomValidator,因为它是为验证而设计的,您必须自己编写代码。

标签: c# html asp.net webforms


【解决方案1】:

在 jquery 中你可以这样做

 $(yourform).submit(function(event){
event.preventDefault();
//do somehting 
})

【讨论】:

    【解决方案2】:

    这不是经典的 asp,它是 asp.net 网络表单,我已经相应地编辑了标签

    在 webforms 中添加输入验证非常简单,您可以为每个表单控件分配一个验证控件,例如

    Contact Name<br />
            <asp:TextBox ID="Contact_name"  runat="server" Width="246 pt"></asp:TextBox>
            <asp:RequiredFieldValidator ID="ContactNameValidator" runat="server" 
                ErrorMessage="Please enter your name" ControlToValidate="Contact_name"></asp:RequiredFieldValidator><br />
            <br />
    Contact Telephone<br />
            <asp:TextBox ID="Contact_phone" runat="server" Width="246pt"></asp:TextBox>
            <asp:RequiredFieldValidator ID="ContactPhoneValidator" runat="server" ControlToValidate="Contact_phone"
                ErrorMessage="Please enter your telephone number"></asp:RequiredFieldValidator>
    

    如果您查看输出,您将看到验证实际上是使用(客户端)JavaScript 完成的。 Asp.net 为您生成,您不必自己编写。

    更多信息在这里 https://msdn.microsoft.com/library/a0z2h4sw%28v=vs.100%29.aspx

    【讨论】:

      【解决方案3】:

      在后面的代码中添加...

      LoginButton.Attributes.Add("onclick", "return false;");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-20
        • 2017-01-26
        • 2010-09-14
        • 2016-10-16
        • 1970-01-01
        相关资源
        最近更新 更多