Performing Validations with ASP.NET
RegularExpressionValidator
This validation control is used to parse strings using special characters. This is similar to the concept of Pattern Matching in PERL. This control can be used in situations such as:
- To check for "@" character on an e-mail address (See Listing 1)
- To verify whether the user entered valid Zip code, Telephone number and web address
<%@ Page Language="VB" %>
<html>
<head>
<title>Validation Controls in Action</title>
</head>
<body>
<form runat="server">
<p>
Enter your e-mail address:
</p>
<p>
<asp:TextBox id="TextBox1" runat="server">
</asp:TextBox>
<asp:Button id="Button1" runat="server"
Text="Click here">
</asp:Button>
</p>
<p>
<asp:RegularExpressionValidator
id="RegularExpressionValidator1"
runat="server"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ErrorMessage="Please enter a valid E-mail ID"
ControlToValidate="TextBox1">
</asp:RegularExpressionValidator>
</p>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<html>
<head>
<title>Validation Controls in Action
</head>
<body>
<form runat="server">
<p>
Enter your e-mail address:
</p>
<p>
<asp:TextBox id="TextBox1" runat="server">
</asp:TextBox>
<asp:Button id="Button1" runat="server"
Text="Click here">
</asp:Button>
</p>
<p>
<asp:RegularExpressionValidator
id="RegularExpressionValidator1"
runat="server"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ErrorMessage="Please enter a valid E-mail ID"
ControlToValidate="TextBox1">
</asp:RegularExpressionValidator>
</p>
</form>
</body>
</html>
http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
Moreover, you can also write custom validation statements depending upon your project requirements.
RequiredFieldValidator
<%@ Page Language="VB" %>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
Your Name:
</p>
<p>
<asp:TextBox id="TextBox1" runat="server">
</asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Click here">
</asp:Button>
</p>
<p>
<asp:RequiredFieldValidator
id="RequiredFieldValidator1"
runat="server"
ErrorMessage="The above field should not be left blank"
ControlToValidate="TextBox1"
display="static">
</asp:RequiredFieldValidator>
</p>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
Your Name:
</p>
<p>
<asp:TextBox id="TextBox1" runat="server">
</asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Click here">
</asp:Button>
</p>
<p>
<asp:RequiredFieldValidator
id="RequiredFieldValidator1"
runat="server"
ErrorMessage="The above field should not be left blank"
ControlToValidate="TextBox1"
display="static">
</asp:RequiredFieldValidator>
</p>
</form>
</body>
</html>
Validation Summary
<%@ Page Language="vb" %>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
Enter a first value:
<asp:TextBox id="txtValue1" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator id="Reqfield1"
runat="server"
ErrorMessage="The above field should not be left blank"
ControlToValidate="txtValue1">
</asp:RequiredFieldValidator>
</p>
<p>
Enter a second value:
<asp:TextBox id="txtValue2" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator id="reqField2"
runat="server"
ErrorMessage="The above field should not be left blank"
ControlToValidate="txtValue2">
</asp:RequiredFieldValidator>
</p>
<p>
<asp:Button id="btnClick" runat="server" Text="Click Here">
</asp:Button>
</p>
<p>
<asp:ValidationSummary id="Validsummary"
runat="server" HeaderText="Errors are as follows:"
ShowMessageBox="True">
</asp:ValidationSummary>
</p>
</form>
</body>
</html>
Figure 2 - Click here for larger image
[C#]
<%@ Page Language="C#" %>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
Enter a first value:
<asp:TextBox id="txtValue1" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator id="Reqfield1"
runat="server"
ErrorMessage="The above field should not be left blank"
ControlToValidate="txtValue1">
</asp:RequiredFieldValidator>
</p>
<p>
Enter a second value:
<asp:TextBox id="txtValue2" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator id="reqField2"
runat="server"
ErrorMessage="The above field should not be left blank"
ControlToValidate="txtValue2">
</asp:RequiredFieldValidator>
</p>
<p>
<asp:Button id="btnClick" runat="server" Text="Click Here">
</asp:Button>
</p>
<p>
<asp:ValidationSummary id="Validsummary"
runat="server" HeaderText="Errors are as follows:"
ShowMessageBox="True">
</asp:ValidationSummary>
</p>
</form>
</body>
</html>
From the above discussion, it is clear that all the required stuffs for validating Graphical User Interface (GUI) controls is now available under a single umbrella of Microsoft .NET. Once you learned the usage of all the controls, it is very easy to implement them, be it a WinForm or an ASP.NET WebForm.
No comments:
Post a Comment