Click here to Skip to main content
15,893,588 members
Articles / Web Development / HTML

Enhancing the presentation of standard validator outputs

Rate me:
Please Sign up or sign in to vote.
4.68/5 (26 votes)
13 Jul 20068 min read 130.2K   758   61  
How flexible is the normal functionality of ASP.NET validators? In this article, I am going to show how to customize the appearance of the attached control of a validator during an error situation on the server or the client side, or even call a custom client function without postback.
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Highlight Validators</title>
    <style>
    .field_error_1 {
        border: 1px dotted red;
        background-color: yellow;
    }
    .field_error_2 {
        border: 1px dotted green;
        background-color: yellow;
    }
    #SummaryValidation {
        font-family: Verdana;
        font-size: 9pt;
        position: absolute;
        top: 200px;
        display: none;
        margin-left: auto;
	    margin-right: auto;
	    width: 100%;
    }
    #SummaryValidation span {
        margin-left: auto;
	    margin-right: auto;
	    width: 300px;
	    border: 1px solid black;
        background-color: #f4f4f4;
        padding: 10px;
        display: block;
    }
    </style>
    <script type="text/javascript">
    function CheckForm()
    {
        if (!Page_IsValid)
        {
            var s = "";
            for (i=0; i<Page_Validators.length; i++) {
                if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].errormessage) == "string") {
                    s += "<li>" + Page_Validators[i].errormessage + "</li>";
                }
            }
            if (s != "")
            {
                document.getElementById('ValidationProblems').innerHTML = s;
                document.getElementById('SummaryValidation').style.display = 'inline';
            }
        }
    }
    function HideValidationSummary()
    {
        document.getElementById('SummaryValidation').style.display = 'none';
    }
    document.onclick = function()
    {
        HideValidationSummary();
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="SummaryValidation">
    <span>
        <p style="font-weight:bold">The following errors were occured:</p>
        <ul id="ValidationProblems"></ul>
    </span>
    </div>
    <table>
        <tr>
            <td style="width: 117px">Name:</td>
            <td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                ControlToValidate="TextBox2" ErrorMessage="Name is required">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>Number of books:</td>
            <td><asp:TextBox ID="TextBox1" runat="server" Width="32px"></asp:TextBox>
            <uc:HighlightCompareValidator ID="HighlightCompareValidator1" runat="server" 
                ControlToValidate="TextBox1" ErrorMessage="Number of books must be numeric" 
                ToolTip="Number of books must be numeric" ErrorCssClass="field_error_1" Display="None"
                Operator="DataTypeCheck" Type="Integer" />
            <uc:HighlightRequiredValidator id="HighlightRequiredValidator1" runat="Server" 
                ControlToValidate="TextBox1" ErrorMessage="Number of books is required"
                ToolTip="Number of books is required" Display="None"
                ErrorCssClass="field_error_2" />
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td style="height:35px"><asp:Button ID="Button1" runat="server" Text="Submit" /></td>
        </tr>
    </table>
    </form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Ukraine Ukraine
Alexander is freelance web developer with 4 years experience. He has skills in ASP.NET/MS SQL Server, PHP/MySQL, Ruby On Rails, XHTML, CSS. You can contact with me by seigo.ua@gmail.com. Also check my homepage at www.klalex.com.

Comments and Discussions