Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Validation - Validating with RegExs using ASP.NET 2.0 and C# .NET
Validating with RegExs using ASP.NET 2.0 and C# .NET


ASP.NET Validation Tutorial

This tutorial will show you how to use the RegularExpressionValidator control using ASP.NET 2.0 and C#.NET

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

The .NET Framework offers a number of controls that simplifies the process of validating user input on a web form. One such control is the RegularExpressionValidator. This control allows you to specify an arbitrary regular expression to validate the input to any one control.

To perform RegEx validation using this control we do not need to be using any extra namespaces. All we need to do is add a RegularExpressionValidator to the web form. We'll put our code in the btnSubmit_Click() event.

When the btnSubmit_Click() event fires it sets the ValidationExpression expression property of the validator to match any decimal number with an optional negative or postive sign in front of it. After clicking the Submit button, the user will be notified when they have not entered a valid decimal number in the textbox.

Note that we must set the ControlToValidate property of the validator at design time to txtDec. Without setting this property you will receive a run-time exception.

protected void btnSubmit_Click(object sender, EventArgs e)
{
regexValidator.ValidationExpression = @"[-+]?\d+\.\d+";
lblStatus.Text += "Validating RegEx: " + regexValidator.ValidationExpression + "\r";
}

We have two textboxes, a submit button, and a label on the front end for user interaction. The front end .aspx page looks something like this:

<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">
Regex Validating an Email:
</td>
<td align="center" bgcolor="#FFFFFF">
Please enter a valid decimal number :<asp:TextBox ID="txtDec" runat="server" Height="14px" Width="176px"></asp:TextBox><br />
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" /><br />
<asp:RegularExpressionValidator ID="regexValidator" runat="server" ControlToValidate="txtDec"
ErrorMessage="This not a valid decimal number"></asp:RegularExpressionValidator>&nbsp;<br />
&nbsp;<asp:label ID="lblStatus" runat="server"></asp:label>
</td>
</tr>
</table>

The flow for the code behind page is as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
regexValidator.ValidationExpression = @"[-+]?\d+\.\d+";
lblStatus.Text += "Validating RegEx: " + regexValidator.ValidationExpression + "\r";
}

}


Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!






 
  Developer Resources







Server Intellect Rocks