Hide redundant error message in ASP.Net ValidationSummary

前端 未结 1 1310
执念已碎
执念已碎 2021-01-26 08:38

I have a asp.net page accepting two input values – Name and Address. Both of them are required fields. I have used required field validators and validation summary.

Whe

相关标签:
1条回答
  • 2021-01-26 08:52

    This is a little hackish, but it'll work:

    Add the following Javascript function:

    function submitValidate() {
        var isValid = Page_ClientValidate('');
    
        if (!isValid) {
            setTimeout("$('#vsumAll ul li:not(:first)').remove()", 5);
        }
    
        return isValid;
    }
    

    In your submit button add this:

    <asp:Button runat="server" ID="btnSave" Text="Save" OnClientClick="submitValidate();"/>
    

    And finaly, make sure you have ClientIDMode="Static" on your ValidationSummary

    Explanation:
    It uses JQuery to remove all but the first li in the ValidationSummary - which is actually an UnorderedList (e.g. ul).
    I put it in a 5ms setTimeout since we want it to run only after the ValidationSummary finished adding all the items to the ul.
    The code will only run if Page_ClientValidate fails - this is the function that performs the ClientSide validation.

    0 讨论(0)
提交回复
热议问题