DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Validating with RegExs using ASP.NET 2.0 and C# .NET

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

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.

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

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>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

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";
}

}




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

Looking for more ASP.NET Tutorials? Click Here!
Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!