Introduction to ASP.NET RegularExpressionValidator
ASP.NET RegularExpressionValidator is one of the key features for validating input values passing from any web-based or desktop-based application by the end-user, all the programming languages have their own use of these features, similarly, ASP.NET also has the similar features to identify the same kind of regular expression input values, which basically holding some unexpected or expected patterns based on the feature of the critical application and ensuring of returning proper values after matching with the define patterns provided by the developer as per application business needs.
Syntax
There have several syntaxes available for ASP.NET RegularExpressionValidator. The following can be some popular or regularly used syntax for a regular expression in the ASP.NET.
- \b: normally considering for backspace.
- \t: normally considering for tab.
- \r: normally returning carriage.
- \v: normally represent vertical tab.
- \f: normally considering as form feed.
- \n: normally considering a new line.
- \: considering as an escape character.
Metacharacters of ASP.NET RegularExpressionValidator
Not only a single character, but a regular expression for the ASP.NET RegularExpressionValidator can also consider for the class of characters as well. These kinds of characters have one so-called name ‘metacharacters’.
- : blank is indicating all the characters excluding \n means newline.
- [abcd]: this one indicating a set of characters for matching with the expected input parameter.
- [1-8a-nA-N]: It is one of the very interesting in regular expression functionality for ASP.NET application. This one always targets a specific character between the range specified. In this case, any number between 1 to 8 or any alphanumeric character A to M. Here capital and small both the letters are considered in matching logic.
- \w: This one also used to identify any kind of require alphanumeric character. It also considers underscoring as the matching character.
- \W: It is indicating the character which basically not a word. So, if anything passing as input value which is not a word, this kind of regular expression might help in the case of an ASP.NET application.
- \s: This is kind of indicating any kind of character considering as white space. If someone willing to identify space or a new line or tab, this character can be useful for the developer. For identity, the new line has one specific regular expression in ASP.NET integration, but if a developer needs logic to identify all the white spaces passing as input values then it can be easily the usable regular expression in any ASP.NET development.
- \S: Have a small difference with earlier regular expression, earlier one was a small letter and this one is a capital letter. In this case, we can able to identify those type of words which are non-whitespace.
- \d: This is used for identifying any kind of decimal character. It is very common for any ASP dot net application to identify any kind of decimal character like 1.2, 1.4, etc.
- \D: This is also just exactly the opposite of the above one. It mainly used for identifying non-decimal characters. It is one of the common and popular regular expression used by ASP dot net application.
- Quantifier: this is also one of the key features for ASP.NET application regular expression functionality. Verities quantifiers have been used for identifying how many times that specific words mentioned in the input values. Example of quantifiers are:
*: It is indicating matching zero or more than that.
+: It is indicating matching with zero or more than that.
?: It is indicating matching zero or maximum one.
{N}: It is indicating matches with n number of characters.
{N,}: It is indicating matches with a number of characters or more than n number of characters.
{N, M}: It is indicating matches count can be in between N number and M number.
The main syntax of using the same in ASP.NET application is:
<asp:RegularExpressionValidator ID="string" runat="server" ErrorMessage="string" ValidationExpression="string" ValidationGroup="string">
<asp:RegularExpressionValidator>
There have multiple properties for regular expression of ASP.NET application, which can be used for different purposes of application requirements.
Properties are:
- AccessKey: It is mainly using for getting control on the keyboard. In short, it can consider a shortcut of the keyboard control.
- BackColor: Mainly used for setting the color of the background.
- BorderColor: Mainly using forgiven one color in the border.
- Font: Using for defining specific font, mainly for controlling the text.
- ForeColor: Using for text color settings.
- Text: Mainly used for display text.
- Tooltip: It is one of the texts which displays on mouse hover.
- Visible: mainly for handling the visibility of the text.
- Height: handling the height of the specific field.
- Width: Handling the width of the specific field.
- ErrorMessage: Setting a specific message which will come as alert during any validation failed.
- ControlToValidate: This is one of the ID, which utilizes to control the same.
- ValidationExpression: It is one of the key property, where we can push the regular expression which needs to validate.
Example to Implement ASP.NET RegularExpressionValidator
Below is the example mentioned :
Example #1
Developers have explicitly introduced one specific text box for email ID where regular expression will default checking before moving to the backend server-side.
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RegularExpressionDemo.aspx.cs"
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style2">Email_ID</td>
<td>
<asp:TextBox ID="username" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="username" ErrorMessage="Please enter valid email" ForeColor="Red" ValidateExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
<asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="auto-style2"></td>
<td>
<br/>
<asp:Button ID="Button1" runat="server" Text="save"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Output:
Open Microsoft visual studio 2010àclick newà project
Click “web” under visual C# installed template paneàselect .NET Framework 3.5àselect ASP.NET Web application.
Write the project name as “RegularExpressionDemo”à project locationàclick “ok”
The new project found under solution explorer
Write click on the projectàclick addàclick “New item”
Name givenà RegularExpressionDemo.aspxà then click the add button
Write code in .aspx file
Set .aspx file as start page.
Click on the green button or click F5 button.
New web project appears at browserà
Give input invalid mail and click on save button
The below error message will appear.
Conclusion
A regular expression is always one of the key features for any kind of web-based application. It gives a big utility in terms of security and avoiding some critical vulnerability for some important data handling applications. This validator very much useful specifically in ASP.NET application, as it can be very easy to use just using that specific tag.
Recommended Articles
We hope that this EDUCBA information on “ASP.NET RegularExpressionValidator” was beneficial to you. You can view EDUCBA’s recommended articles for more information.