473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Regular expressions and commas

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
3 7614
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Darren Dale | last post by:
I'm stuck. I'm trying to make this: file://C:%5Cfolder1%5Cfolder2%5Cmydoc1.pdf,file://C %5Cfolderx%5Cfoldery%5Cmydoc2.pdf (no linebreaks) look like this: ../mydoc1.pdf,./mydoc2.pdf my...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
2
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
7
by: G .Net | last post by:
Hi I was wondering if you could help me with the following: I have a string e.g. "#54x454#,#22b6#,#885333#". I want to be able to store in an array list, the following from the string: ...
0
by: peridian | last post by:
Hi, I wanted a web page where I could post code to, and have it appear in coloured formatting based on the context of the code. Most of the techniques I have seen for this involve complex use...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
2
by: Yimin Rong | last post by:
For example, given a string "A, B, C (P, Q, R), D (X, Y , Z)". Would like to split into tokens thusly: a == "A" a == "B" a == "C (P, Q, R)" a == "D (X, Y , Z)"
1
Mike Kypriotis
by: Mike Kypriotis | last post by:
I want to make a check (propably with regular expressions) that the flashvars inserted by the user are of the right format, so we have pairs seperated by commas (e.g....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.