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.NET, How 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:
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.NET, How 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.
You can download the code by clicking on the below Download image.
No comments:
Post a Comment