Wednesday, 27 July 2011

Validating with RegExs using ASP.NET 2.0 and VB.NET


This tutorial will show you how to use the RegularExpressionValidator control using ASP.NET 2.0 and VB.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 Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
regexValidator.ValidationExpression = "[-+]?\d+\.\d+"
lblStatus.Text &= "Validating RegEx: " & regexValidator.ValidationExpression + Constants.vbCr
End Sub

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

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.

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
regexValidator.ValidationExpression = "[-+]?\d+\.\d+"
lblStatus.Text &= "Validating RegEx: " & regexValidator.ValidationExpression + Constants.vbCr
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment