//
you're reading...
ASP Classic, ASP.NET, Drupal, IT-Center, Javascript, PHP, VB.NET, Web Based

Sample Pattern Regular Expression (REGEX)

Title TestDetailsPattern Title
Expression
^\$[0-9]+(\.[0-9][0-9])?$
Description
Validates a dollar amount including a dollar sign and 2 decmals. The decimal and cents are optional.
Matches
$1.50 | $49 | $0.50
Non-Matches
1.5 | $1.333 | this $5.12 fails
Title TestDetailsPattern Title
Expression
^(\d{1,3}'(\d{3}’)*\d{3}(\.\d{1,3})?|\d{1,3}(\.\d{3})?)$
Description
This regex match numeric data in the following format: thousands are separated by (‘) apostrophe, decimal places are separated by dot (.) Maximum three decimal places are not required. It’s easy to change to other separators as well.
Matches
1’235.140 | 1’222’333.120 | 456
Non-Matches
1234.500 | 78’45.123 | 123,0012
Title TestDetailsPattern Title
Expression
^\d{0,2}(\.\d{1,2})?$
Description
This regular expression validates that the data entered is a number with a maximum of two integers and two decimals and a minimum of one integer or one decimal.
Matches
99.99 | 99 | .99
Non-Matches
999.999 | 999 | .999
Title TestDetailsPattern Title
Expression
^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$
Description
This will grep for a valid MAC address , with colons seperating octets. It will ignore strings too short or long, or with invalid characters. It will accept mixed case hexadecimal. Use extended grep.
Matches
01:23:45:67:89:ab | 01:23:45:67:89:AB | fE:dC:bA:98:76:54
Non-Matches
01:23:45:67:89:ab:cd | 01:23:45:67:89:Az | 01:23:45:56:
Title TestDetailsPattern Title
Expression
^(\d|,)*\.?\d*$
Description
Matches Numeric with Commas and a single decimal point. Also matches empty string.
Matches
1,000 | 3,000.05 | 5,000,000
Title TestDetailsPattern Title
Expression
^(\d|-)?(\d|,)*\.?\d*$
Description
Input for Numeric values. Handles negatives, and comma formatted values. Also handles a single decimal point
Matches
5,000 | -5,000 | 100.044
Non-Matches
abc | Hundred | 1.3.4

See Also : http://regexlib.com/Search.aspx?k=decimal&c=-1&m=-1&ps=20&AspxAutoDetectCookieSupport=1
_______________________________________________________________________________
The following code sample shows how to write validationexpression for a RegularExpressionValidator in ASP.NET

Email validation

<asp:TextBox ID="txtEmail" runat="Server" Columns="40" />
<asp:RegularExpressionValidator ID="revemail" runat="server" ControlToValidate="txtEmail" ErrorMessage="Eg:abcde@abc.ad" validationexpression="^.+\@.+\..+$"  Display="static" SetFocusOnError="true"></asp:RegularExpressionValidator>

Pincode validation

<asp:TextBox ID="txtPincode" runat="Server" Columns="40" />
<asp:RegularExpressionValidator ID="revPincode" runat="server" ControlToValidate="txtPincode" ErrorMessage="enter correct pincode" validationexpression="^.+\@.+\..+$"  Display="static" SetFocusOnError="true"></asp:RegularExpressionValidator>

validating password and confirm password

<asp:TextBox ID="TextBox3" runat="Server" TextMode="Password" Columns="40" />
<asp:TextBox ID="textbox2" runat="Server" TextMode="Password" Columns="40" />
 <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Passwords don't match!" ControlToValidate="TextBox3" 
        ControlToCompare="textbox2" ></asp:CompareValidator>

Validating year

<asp:TextBox ID="txtValidityDate" runat="server" />
 <asp:RegularExpressionValidator ID="revvaliditydate" runat="server" ControlToValidate="txtValidityDate" ErrorMessage="Date Format(dd/mm/yyyy)" 
                            ValidationExpression="^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$" Display="static" SetFocusOnError="true"></asp:RegularExpressionValidator>

Validation For 999-99-9999 Format

ValidationExpression="(^\d{3}\-?\d{2}\-?\d{4}$)"

Validation For(555) 555-5555 Format

ValidationExpression="(^((\(?\d{3}\))|(\d{3}))?\s?\d{3}[\-.]?\d{4}$)"

About berbagisolusi

Berbagi merupakan sebuah bentuk simbol keikhlasan untuk membantu dan menolong, sedangkan solusi adalah cara menyelesaikan masalah. Setiap manusia pasti mengalami masalah, tetapi kita tidak perlu mengalami masalah yang sama jika orang lain pernah mengalami dan kita tahu hal tersebut.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

December 2011
M T W T F S S
« Nov   Jan »
 1234
567891011
12131415161718
19202122232425
262728293031  

Archives

Web Statistic

Blog Stats

  • 149,580 hits
%d bloggers like this: