This tutorial will show you how to use the RegularExpressionValidator control using ASP.NET 2.0 and C#.NET.
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.
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";
} |
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect
and have been very happy. They are the most professional, customer
service friendly and technically knowledgeable host we've found so far.
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> <br />
<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";
}
} |
|