Sunday, 29 June 2014

How to show validation control's Error messages in Alert box in ASP.NET

Hi friends,in this article I will explain about How to show validation control's Error messages in Alert box in ASP.NET.
I already explained in the previous articles about ASP.NET Validation Controls with example | How to use ControlValidator in ASP.NET | ControlValidator Control in ASP.NETHow to Upload Multiple Files Using FileUpload Control in ASP.NET using C#/VB.NET and jQuery:How to check all checkboxes except one checkbox || jQuery :not() Selector Example
Now we will know about how to show validation control's Error messages in Alert box in ASP.NET

ASP.NET:
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to show validation control's Error messages in Alert box in ASP.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <table>
        <tr>
            <td>
                <asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtName" runat="server" ValidationGroup="Validation"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvName" runat="server" ControlToValidate="txtName"
                    Display="None" ErrorMessage="Name is Required" ValidationGroup="Validation"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblGender" runat="server" Text="Gender"></asp:Label>
            </td>
            <td>
                <asp:DropDownList ID="ddlGender" runat="server" ValidationGroup="Validation">
                    <asp:ListItem Selected="True" Value="0">--Select--</asp:ListItem>
                    <asp:ListItem Value="1">Male</asp:ListItem>
                    <asp:ListItem Value="2">Female</asp:ListItem>
                </asp:DropDownList>
                <asp:CompareValidator ID="CVGender" runat="server" ControlToValidate="ddlGender"
                    Display="None" ErrorMessage="Gender is Required" Operator="NotEqual" ValidationGroup="Validation"
                    ValueToCompare="0"></asp:CompareValidator>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblEmail" runat="server" Text="Email Address"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtEmail" runat="server" ValidationGroup="Validation"></asp:TextBox>
                <asp:RegularExpressionValidator ID="regEmail" runat="server" ControlToValidate="txtEmail"
                    Display="None" ValidationGroup="Validation" ErrorMessage="Enter Valid Email address "
                    ValidationExpression="^([a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]){1,70}$">
                </asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblValidation" runat="server" Text="* Marked fields are mandatory"
                    ForeColor="Red"></asp:Label>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnShowAlert" runat="server" Text="Show Alert" ValidationGroup="Validation" />
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
                    ShowSummary="False" ValidationGroup="Validation" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>


The output of the above code as shown in the below figure.
validation control's Error messages in Alert box in ASP.NET
You can download the code by clicking on the below Download image. 

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.