locked
Validation Message RRS feed

  • Question

  • User-797751191 posted

    Hi

      I have below code & i want on Cancel button it should not display message , Currently when Document No left blank it says Please fill this field

    <div class="row">
                        <div class="form-group col-md-4 has-error">
                            <label>Document No *</label>
                            <asp:TextBox ID="txtInvoiceNo" runat="server" class="form-control"  placeholder="Enter Document No" required="true"></asp:TextBox>
                        </div>
    
                        <div class="form-group col-md-4 has-error">
                            <label>Date *</label>
                            <asp:TextBox ID="txtPostingDate" runat="server" class="form-control datepicker"   placeholder="Document Date" ></asp:TextBox>
                        </div>
    
                    </div>
    
    
    <div class="row">
                        <div class="form-group col-md-12" style="text-align:center">
                            <asp:Button ID="btnSave" runat="server" class="btn btn-primary" Text="Update & Print" Enabled ="false" OnClick="btnSave_Click"/>
                            <asp:Button ID="btnCancel" runat="server" class="btn btn-primary" Text="Cancel" causesvalidation="false" OnClick="btnCancel_Click"/>
    
                        </div>
                    </div>

    Thanks

    Saturday, August 10, 2019 8:50 AM

All replies

  • User475983607 posted

    I have below code & i want on Cancel button it should not display message , Currently when Document No left blank it says Please fill this field

    You are using HTML 5 validation which is a browser feature.  By the way your syntax is incorrect.  

    <asp:TextBox ID="txtInvoiceNo" runat="server" class="form-control"  placeholder="Enter Document No" required="true"></asp:TextBox>

    By the way your syntax is incorrect.  

    <asp:TextBox ID="txtInvoiceNo" runat="server" class="form-control"  placeholder="Enter Document No" required></asp:TextBox>

    I recommend using standard ASP.NET validation controls and validation groups rather than HTML 5 validation.  See the standard ASP.NET Web Forms reference documentation.

    https://blogs.msdn.microsoft.com/codefx/2009/09/08/how-to-use-the-asp-net-validation-control-to-validate-the-user-input/

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.basevalidator.validationgroup?view=netframework-4.8

    If you insist on HTML 5 validation then write JavaScript to remove the required attribute when the button is clicked.

    https://stackoverflow.com/questions/23646844/jquery-ignore-html5-input-required

    Saturday, August 10, 2019 10:40 AM
  • User288213138 posted

    Hi jsshivalik,

    If you don't want to display message, there are two ways to do it.

    The first method is to set the causesvalidation attribute to true.  the causesvalidation attribute gets or sets a value indicating whether validation is performed when the Button control is clicked.

    If you don't want to display message , but want to validate the Textbox, you can try to use RequiredFieldValidator control.

    The code:

    <div>
                
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server" ControlToValidate="TextBox1" ></asp:RequiredFieldValidator><br />
                 
                <asp:Button ID="bt1" runat="server" Text="button"/>
            </div>

    Best regards,

    Sam

    Monday, August 12, 2019 10:40 AM