简体   繁体   中英

use html5 validator in asp.net webform when have two Button runat server

<body>
<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtb3" runat="server" required></asp:TextBox>
        <asp:TextBox ID="txtb2" runat="server" required></asp:TextBox>
        <asp:TextBox ID="txtb1" runat="server" required></asp:TextBox>

        <asp:Button ID="btnBack" runat="server" Text="btn 2" OnClick="btnBack_Click" />
        <asp:Button ID="btnSubmit" runat="server" Text="btn 1" OnClick="btnSubmit_Click" />

    </div>
</form>

This is my code page. I have two button and need validation in submit button but don't need validate in back button. What to do that!!!

You can write javascript to prevent that for back button. Following is the snippet you need.

<script  type="text/javascript">
    function setNoValidate() {
        for (var f = document.forms, i = f.length; i--;) f[i].setAttribute("novalidate", i);
        document.forms[0].submit(); // replace 0 with with actual form name, like document.formSub
    }
</script>

<asp:Button ID="btnBack" runat="server" Text="btn 2" OnClientClick="setNoValidate();" />

Update

You can use HTML5 attribute formnovalidate to escape HTML5 validation ie directly use <asp:Button ID="btnBack" runat="server" Text="btn 2" formnovalidate /> instead of above script. Explained here

Note: CausesValidation is to handle validation for asp.net validators not HTML5 validation.

Use formnovalidate tag to avoid validation ---

formnovalidate="formnovalidate"

 <asp:Button ID="btnBack" runat="server" Text="btn 2" OnClick="btnBack_Click" formnovalidate="formnovalidate" />

<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtb3" runat="server" required></asp:TextBox>
            <asp:TextBox ID="txtb2" runat="server" required></asp:TextBox>
            <asp:TextBox ID="txtb1" runat="server" required></asp:TextBox>

            <asp:Button ID="btnBack" runat="server" Text="btn 2" OnClick="btnBack_Click" formnovalidate="formnovalidate" />
            <asp:Button ID="btnSubmit" runat="server" Text="btn 1" OnClick="btnSubmit_Click"  />

        </div>
    </form>

This is the right way to do :=)

<asp:Button ID="btnBack" runat="server" Text="btn 2"
      OnClick="btnBack_Click"  CausesValidation="False"/>

只需在您不希望触发html5验证的按钮上添加formnovalidate =“ formnovalidate”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM