Regular Expressions in ASP.NET

Regular expressions are an extremely useful tool for working with text. Whether you need to validate user input, search for patterns within strings, or reformat text in powerful ways.

Here is some example:

This is regular expression for comma[,] and semicolon[;] saperated multiple email address like gmail

\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w{2,4}([-.]\w{2,4})*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w{2,4}([-.]\w{2,4})*)*

<asp:TextBox ID="txtEmail" runat="server" CssClass="textbox" TextMode="MultiLine" Width="250px" Height="100px"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" ControlToValidate="txtEmail"
Display="Dynamic" ErrorMessage="email required" ValidationGroup="Scheme"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revEmail" ValidationGroup="Scheme" Display="Dynamic"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w{2,4}([-.]\w{2,4})*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w{2,4}([-.]\w{2,4})*)*" ControlToValidate="txtEmail"
runat
="server"
ErrorMessage="Enter data in email format"></asp:RegularExpressionValidator>

Some Currency Formats:

Currency (non- negative)

A) \d+(\.\d\d)?$ Example(1.00)
B) ^\d+(\.\d+)?$ Example(512.222222)

Validates a positive currency amount. If there is a decimal point, it requires 2 numeric characters after the decimal point. For example, 3.00 is valid but 3.1 is not.

Currency (positive or negative)

A) ^(-)?\d+(\.\d\d)?$ Example (1.20)

Validates for a positive or negative currency amount. If there is a decimal point, it requires 2 numeric characters after the decimal point.

Currency (with $ or without $)

A) ^(^(\$)?\d+(\.\d+)?$|^(-)?\d+(\.\d+)?$)$ Example ($1.20 or 1.20)

Custom Regular Expression Validation (Float Number validation start with $ symbol or without $ symbol)

Currency separated by ,With Or Without $ Symbol

A) ^(^(\$)?\d+(\.\d+)?$|^(-)?\d+(\.\d+)?$)$|^(^(\$)?[^\d\d\,]*\d\d\d(\.\d+)?$|^(-)?\d+(\.\d+)?$)$

Example $11,222.33 Or 11,11,111.22

Value with or without money format using (, and $ sign)

Currency separated by , With $ symbol with support following example format.

A) ^(^(\$)?\d+(\.\d+)?$|^(-)?\d+(\.\d+)?$)$|^(^(\$)?((\d\d\d\,){1,4}|(\d\d\,){1,4}|(\d\,){1,4}){1,4}\d\d\d(\.\d+)?$|^(-)?\d+(\.\d+)?$)$

Example

$1,111.00
$11,111.00
$111,111.00

Some String Formats:

Only alphabets

A) ^[a-zA-Z]*$ Example:- Manoj

Insert only characters

Only alphabets And Numbers

 ^[a-zA-Z0-9]*$ Example :-Rajpal123

Please enter character or character and numbers only, not space, not special character

Only character And number And spaces

([a-zA-Z0-9\s]){8,20} Example :- ram 234

Please enter min 8 and max 20 character with spaces.

User name

^[a-zA-Z''-'\s]{33,57}$ Example John Ooe B'Dell

Validates a name. Allows up to 40 uppercase and lowercase characters and a few special characters that are common to some names. You can modify this list.

Password

A) (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$ Example 11RamGopal

Validates a strong password. It must be between 8 and 10 characters, contain at least one digit and one alphabetic character, and must not contain special characters.

Some Number Formats:

Float number with + Or - Symbol

A) ^[-+]?[0-9]*\.?[0-9]*$ Example 1.222 Or -1.222

Integer Value (No Limit)

^(\d+)?$ Example :- 125465896532365485

Custom Regular Expression Validation (Integer Number validation) user can input only number but no limit

Integer Value (WithLimit)

A) ^(\d\d\d)?$ Example:- 123

Enter Only three digit

Float Number with % symbol or without % Symbol at end of the digit

^(^(-)?\d+(\.\d+)?$|^\d+(\.\d+)?(\%)?$)$ Example :- 2% or 2.20% or 2 or 2.20

Custom Regular Expression Validation (Float Number validation End with % symbol or without % symbol)

Some File up loader Regular expression Formats:

File uploader

^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.gif|.GIF|.jpg|.JPG|.jpeg|.JPEG)$ Example:- abc.jpg

Provide to upload file only in given extension in regular expression.

Ebook Download
View all
Learn
View all