By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

Regular expressions and commas

P: n/a
Hi.

I have a regularexpression validator control on a page. This regular
expression validates a textbox to accept only numbers and commas:
validationexpression="[\d,]*"

I am trying to modify this expression to not allow commas at the
beginning and at the end of the expression without success. It needs
to allow commas only between the numbers. How can I do that?

Thanks,
Robert Scheer
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hey Robert. How about this (untested) one?

((\d{1,2},)?(\d{3},)*(\d{3}))|(\d+)

It's been a while since I've crafted a regex, but I believe that will
allow for (1 or 2 digits, followed by a comma (optionally)), followed by
0 to many groups of 3 digits, followed by a comma, followed by 3 digits.
OR just 1 to many digits.

The place to go, though, is http://regexlib.com. They have pre-made
regular expressions for a bevy of common scenarios.

hth

--

Scott Mitchell
mi******@4guysfromrolla.com
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
Nov 18 '05 #2

P: n/a
How about this one:

(\d)+,((\d)+,)*(\d)+

While working with regular expressions, you may find the following tool
helpful :

http://www.weitz.de/regex-coach/

Tor Bådshaug
tor.badshaug [//at\\] bekk.no.
Nov 18 '05 #3

P: n/a
It sounds like you want to validate decimal values with the thousand's
separator (1,200,300.45) and discovered that CompareValidator
(Operator=DataTypeCheck, Type=Decimal) doesn't handle the decimal places.

The regular expression has its faults. Mostly, it cannot really determine if
a valid number is entered. It just looks at string patterns. The
CompareValidator actually parses the text and converts it into a decimal
value. If that conversion fails, it reports an error. By converting it to
decimal, it also allows comparisons like comparing to another number as
offered by the RangeValidator and CompareValidator.

Considering this, you may want to use a technique that converts to a decimal
value. Here are some ideas:
1. Use a CustomValidator and implement server side validation that uses
..net's Decimal.Parse() method. Going further, write a client-side evaluation
function that strips out all commas then uses javascript's parseFloat()
function:
var vVal = parseFloat(vText);
if (isNaN(vVal))
// its an error

2. My replacement to Microsoft's validators, Professional Validation And
More, automatically supports thousands separators in its own versions of
RangeValidator and CompareValidator. It handles client side validation
properly and works on many more browsers.
(http://www.peterblum.com/vam/home.aspx) It also provides a DecimalTextBox
and CurrencyTextBox that filter keystrokes and reformats, inserting
thousands separators if you like.

--- Peter Blum
www.PeterBlum.com
Email: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Robert Scheer" <rb******@my-deja.com> wrote in message
news:cf**************************@posting.google.c om...
Hi.

I have a regularexpression validator control on a page. This regular
expression validates a textbox to accept only numbers and commas:
validationexpression="[\d,]*"

I am trying to modify this expression to not allow commas at the
beginning and at the end of the expression without success. It needs
to allow commas only between the numbers. How can I do that?

Thanks,
Robert Scheer

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.