Brought from: http://salearningschool.com/displayArticle.php?table=Articles&articleID=1325&title=ASP.Net%20Validation%20Control%20Examples%20in%20C#.
ASP.Net Validation Control Examples. Just check the code below
Some notes
- RequiredFieldValidator: is used to check that a field is filled up
- CompareValidator: Compare the value of a field with another field or data
- RangeValidator: Compares the data is within a given range
- RegularExpressionValidator: Domain name syntax, email addtress syntax
- ValidationSummary: Display all validation errors in a summary box
- CompareValidator: Write your own custom validation rules and display the outcome
- Remember
- You can display error on the side of the control
- You can display error under the control
- The example used both just for example
- The Text property is the output that comes just where the validation control is placed. ErrorMessage propert is for the ValidationSummary
- If you just want the Validation Summary output, use Display=”none” for the validation control (not the summarycontrol) itself
- the code can be seen at http://salearningschool.com/samples/validate.txt as well
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="validation.aspx.cs"
Inherits="validation" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Validation Control</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ValidationSummary ID="ValidationSummary1"
HeaderText="Form Validation Summary"
DisplayMode="BulletList"
EnableClientScript="true"
ForeColor = "Red"
runat="server"
/>
<asp:Label ID="lblFirstName" runat="server" Text="First Name:"
Width="120"></asp:Label>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="firstNameValidator"
ControlToValidate="txtFirstName"
ErrorMessage="First Name Required"
InitialValue=""
Text=""
Display ="None"
runat="server"/>
<br />
<asp:Label ID="lblLastName" runat="server" Text="Last Name" Width="120">
</asp:Label>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<br/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate="txtLastName"
ErrorMessage="Last Name Required"
InitialValue=""
Text="Last Name Required"
ForeColor="Red"
runat="server"/>
<br />
<asp:Label ID="lblEmail" runat="server" Text="Email:" Width="120">
</asp:Label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="emailValidation"
runat="server"
ControlToValidate="txtEmail"
ErrorMessage="Invalid Fformat"
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}$"
ForeColor ="Red"
/>
<br />
<asp:Label ID="lblPassword" runat="server" Text="Password" Width="120">
</asp:Label>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password">
</asp:TextBox>
<br />
<asp:Label ID="lblPassword2" runat="server" Text="Re Enter Password" Width="120">
</asp:Label>
<asp:TextBox ID="txtPassword2" runat="server" TextMode="Password">
</asp:TextBox>
<br />
<asp:CompareValidator ID="compareV" ControlToValidate="txtPassword"
ControlToCompare="txtPassword2" runat="server" Text="mismatchmatch"
ForeColor="Red" />
<br />
<asp:Button ID="btnSubmit" runat="server" OnClick="Button1_Click" Text="Submit" />
</div>
</form>
</body>
</html>
From: http://sitestree.com/?p=3729
Categories:.Net Web Applications
Tags:
Post Data:2016-07-16 11:12:48
Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a>
In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
<a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>
