2

I am using validation summary control to display error messages of asp.net validation controls.

There are some validations in page for which validation controls are not being used. I am using custom javascript and jquery code for these. Kindly guide how I can display messages of these errors in validation summary control along with asp.net validation controls.

2
  • +1 for the good question. What did you try? Commented Jun 21, 2011 at 14:12
  • Have not tried anything, actually I am not aware of Validation summary client side API. Commented Jun 21, 2011 at 14:16

2 Answers 2

2

The key is to use the Custom Validation control. This control supports client side scripting (so you can still use your javascript code to do it) but still ties into the validation framework asp.net provides.

From the microsoft article, something like this:

<SCRIPT LANGUAGE="JavaScript">
function validateLength(oSrc, args){
   args.IsValid = (args.Value.length >= 8);
}
</SCRIPT>

<asp:Textbox id="text1" runat="server" text="">
</asp:Textbox>

<asp:CustomValidator id="CustomValidator1" runat=server 
      ControlToValidate = "text1"
      ErrorMessage = "You must enter at least 8 characters!"
   ClientValidationFunction="validateLength" >
</asp:CustomValidator>
Sign up to request clarification or add additional context in comments.

1 Comment

after trying several wrong answers around, finally it worked. Thanks.
1

As Patrick said you can add the custom validator controls in place of your custom javascript/jQuery code. And then accordingly you can add the summary validation control. Also you can change the error message in your javascript code. Please check the below code for your reference.

<script type="text/javascript">
function validatetxtLength(source, args)
{
    var txtVal=document.getElementById('<%=txtusername.ClientID %>').value;
    if(txtVal=="")
    {
        document.getElementById('<%=custxtValidator.ClientID %>').setAttribute("errormessage","Please Enter the User Name");
        args.IsValid=false; 
    }
    else if(txtVal.length>9)
    {
        document.getElementById('<%=custxtValidator.ClientID %>').setAttribute("errormessage","Username must have less than 10 characters");
        args.IsValid=false;
    }
    else
    {
        args.IsValid=true;
    }
    return;
}
</script>

<div>
    <table cellpadding="0" cellspacing="0" border="0" width="712px">
        <tr>
            <td colspan="3" align="center">
                Validator Testing
            </td>
        </tr>
        <tr>
            <td>
                Please Enter your User Name:
            </td>
            <td>
                <asp:TextBox ID="txtusername" runat="server" Width="150px"></asp:TextBox>
            </td>
            <td>
                <asp:CustomValidator ID="custxtValidator" runat="server" ErrorMessage="User Name must have less than 10 characters"
                    Text="*" ForeColor="Red" ClientValidationFunction="validatetxtLength"></asp:CustomValidator>
            </td>
        </tr>
        <tr>
            <td colspan="3" align="center">
                <asp:Button ID="btnsubmit" runat="server" Text="Submit" />
            </td>
        </tr>
    </table>
    <asp:ValidationSummary ID="ValidationSummary1" HeaderText="Please check below validations:"
        runat="server" DisplayMode="BulletList" EnableClientScript="true" />
</div>

This might be useful for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.