单击按钮时,ASP.Net验证不适用于文本框

时间:2018-07-10 11:29:03

标签: javascript c# asp.net

我想验证文本框,但是验证控件对按钮单击事件无效。我搜索了很多并应用了不同的方法,但问题仍然存在。任何帮助将不胜感激。谢谢。

function ConvertToImage(btnExport) {
        html2canvas($("#myDIV")[0]).then(function (canvas) {
            var base64 = canvas.toDataURL();
            $("[id*=hfImageData]").val(base64);
            __doPostBack(btnExport.name, "");
        });
        return false;
    }
<asp:Label runat="server" CssClass="lbl" >Product Name</asp:Label>
<asp:TextBox runat="server" ID="custom_Name" Height="50px"  Width="250px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Font-Size="X-Large" ControlToValidate="custom_Name" ErrorMessage="Please Enter Product Name" ValidationGroup="vg"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Only Alphabets are Allowed" Font-Size="X-Large" ControlToValidate="custom_Name" ValidationExpression="[a-zA-Z ]*$" ValidationGroup="vg"></asp:RegularExpressionValidator>
             
              <!--Button for save image start-->
        <asp:HiddenField ID="hfImageData" runat="server" />
<asp:Button ID="btnExport" Text="Export to Image" runat="server" UseSubmitBehavior="false" OnClick="ExportToImage" OnClientClick="return ConvertToImage(this)" ValidationGroup="vg" CssClass="btnstyle" Height="50px" Width="200px" />
    <!--Button for save image ends-->
                    <asp:Button runat="server" Height="50px" Width="200px" BackColor="#ff8000" ForeColor="#ffffff" Text="Customized Designs" CssClass="btnstyle" ID="btnCustom" OnClick="btnCustom_Click" />

2 个答案:

答案 0 :(得分:0)

您还需要将ValidationGroup属性添加到文本框的标记-

<asp:TextBox runat="server" ID="custom_Name" ValidationGroup="vg" Height="50px"  Width="250px"></asp:TextBox>

HTH!

答案 1 :(得分:0)

您需要将CausesValidation =“ true”与组一起添加到按钮。

 <asp:TextBox runat="server" ID="custom_Name" Height="50px" Width="250px" ValidationGroup="vg"></asp:TextBox>
 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Font-Size="X-Large" ControlToValidate="custom_Name" ErrorMessage="Please Enter Product Name" ValidationGroup="vg"/>
 <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Only Alphabets are Allowed" Font-Size="X-Large" ControlToValidate="custom_Name" ValidationExpression="[a-zA-Z ]*$" ValidationGroup="vg"></asp:RegularExpressionValidator>

 <asp:Button ID="btnExport" Text="Export to Image" ValidationGroup="vg" CausesValidation="true" runat="server" UseSubmitBehavior="false" OnClick="ExportToImage" OnClientClick="return ConvertToImage(this)"   CssClass="btnstyle" Height="50px" Width="200px" />