Regular expressions and commas

R

Robert Scheer

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
 
S

Scott Mitchell [MVP]

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
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
P

Peter Blum

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: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top