Solved

Regular Expression for decimal values in an ASP:RequiredFieldValidator

Posted on 2010-08-17
10
1,313 Views
Last Modified: 2013-12-17
Hi:

I'm not great with regular expressions, so I thought I'd do a post here.

I have an  asp:RequiredFieldValidator on an asp:TextBox.
I want to add a regular expression to validate numeric/decimal values.

So, samples which would match the regex would be:
=====================================

123
123.12
123.00
123
1234
1,234
1,234.00
1,234.56
1234456
1,234,456
1,234,456.00
1,234,456.78

Any help would be greatly appreciated.

Thanks,
JohnB
0
Comment
Question by:jxbma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 11

Expert Comment

by:bmatumbura
ID: 33455609
Try this:
^(\d|-)?(\d|,)*\.?\d*$

Open in new window

0
 
LVL 28

Expert Comment

by:sybe
ID: 33455619
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33455625
http://www.regexlib.com/Search.aspx?k=decimal&c=3&m=5&ps=20

Here you got a list of regular expression. You can select or customize the expression which were you want to build. So take a look at the above site you even have some expression which you can just copy into your code and you should be good to go.

Good Luck.
0
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

 
LVL 11

Expert Comment

by:bmatumbura
ID: 33455647
Note: My first post also allows negative numbers and it enforces a comma between groups of 3 digits. Use this to only allow positive numbers and make the comma between digit groups optional: ^(((\d{1,3})(,\d{3})*)|(\d+))(.\d+)?$
0
 
LVL 7

Expert Comment

by:jdavistx
ID: 33455671
I agree with sybe.

You could do something like the attached.  The value of i will only matter if the parse was successful.
int i;
if( Double.TryParse(myValue.ToString(), i) )
	//i now contains the double value of myValue

Open in new window

0
 
LVL 1

Author Comment

by:jxbma
ID: 33456000
I'm going with bmatumbura.
This is client side validation.
Should easily be able to do it with the right regex.

JB
0
 
LVL 1

Author Comment

by:jxbma
ID: 33457130
bmatumbura:

This works fine except for these cases:

1,234
1,234.56
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33458439
It would be a lot simpler just to use a RangeValidator with its Type attribute set correctly.
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33459383
^(\d{1,3},(\d{3},)*\d{3}(\.\d{1,3})?|\d{1,3}(\.\d{3})?)$
try this
0
 
LVL 1

Accepted Solution

by:
jxbma earned 0 total points
ID: 33512971
The best I came up with is:


^[0-9]+(,[0-9]{3})*(\.[0-9]{0,2})*

It works for the following cases:
----------------------------------------
10
10.00
10.10
10.1
100.1
1000.1
123.1
1234.1
1,234.1
10.01
10.
100
101
110
1000
1001
1010
1011
1100
1101
1110
123
123.12
123.00
123
1234
1,234
1,234.00
1,234.56
1234456
1,234,456
1,234,456.00
1,234,456.78
1,234,456.00

Unfortunately, it falsely accepts the following:
---------------------------------------------------------
10.123


But I can live with that for now.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question