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

Regular Expression for currency

P: n/a
Hi All,

I need a regular expression to be used in an ASP page which would
allow a user to enter:

1000 as well as
1,000

i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)

If anyone's aware, please let me know. I am in urgent need to use it.

Thanks and Regards,
Ankur

Mar 29 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Ankur,

Do you have to use a regular expression here? It seems like you have to
use a number, so why not use the TryParse method on the Int32 class? You
can set it to recognize thousands separators, or skip them (using the
NumberFormatInfo class) and then go from there.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<he********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
Hi All,

I need a regular expression to be used in an ASP page which would
allow a user to enter:

1000 as well as
1,000

i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)

If anyone's aware, please let me know. I am in urgent need to use it.

Thanks and Regards,
Ankur

Mar 29 '07 #2

P: n/a
Hi,
The ugliest code ever:

try
{
double d = Convert.toDouble( mycurvalue.Replace( "$", "").Replace(
",","") );

}

Hope you like it :)
<he********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
Hi All,

I need a regular expression to be used in an ASP page which would
allow a user to enter:

1000 as well as
1,000

i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)

If anyone's aware, please let me know. I am in urgent need to use it.

Thanks and Regards,
Ankur

Mar 29 '07 #3

P: n/a
On Mar 30, 1:17 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Ankur,

Do you have to use a regular expression here? It seems like you have to
use a number, so why not use the TryParse method on the Int32 class? You
can set it to recognize thousands separators, or skip them (using the
NumberFormatInfo class) and then go from there.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

<hellben...@gmail.comwrote in message

news:11**********************@l77g2000hsb.googlegr oups.com...
Hi All,
I need a regular expression to be used in an ASP page which would
allow a user to enter:
1000 as well as
1,000
i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)
If anyone's aware, please let me know. I am in urgent need to use it.
Thanks and Regards,
Ankur- Hide quoted text -

- Show quoted text -
Hi again,

Thanks for your response. But I actually need to use regular
expression on aspx page itself so that the user if wishes to enter an
amount with thousand separator, he is allowed to do so. Also the
simple form(not having commas). Please help me with this if you can.

Regards,
Ankur

Mar 30 '07 #4

P: n/a
Thanks for your response. But I actually need to use regular
expression on aspx page itself so that the user if wishes to enter an
amount with thousand separator, he is allowed to do so. Also the
simple form(not having commas). Please help me with this if you can.
And if you will have to localize your web site, or accept money from outside
US, you will run into troubles (because the decimal separator is comma in
most of Europe). TryParse is the safest option.
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Mar 30 '07 #5

P: n/a

<he********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
Hi All,

I need a regular expression to be used in an ASP page which would
allow a user to enter:

1000 as well as
1,000

i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)
Something like:

[0-9]+(,[0-9]{3})*

As others have said, this is only good with the (American? Imperial?)
notation using comma for thousands grouping and point as decimal separator.
>
If anyone's aware, please let me know. I am in urgent need to use it.

Thanks and Regards,
Ankur

Mar 30 '07 #6

P: n/a
On Mar 30, 8:59 pm, "Ben Voigt" <r...@nospam.nospamwrote:
<hellben...@gmail.comwrote in message

news:11**********************@l77g2000hsb.googlegr oups.com...
Hi All,
I need aregularexpressionto be used in an ASP page which would
allow a user to enter:
1000 as well as
1,000
i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)

Something like:

[0-9]+(,[0-9]{3})*

As others have said, this is only good with the (American? Imperial?)
notation using comma for thousands grouping and point as decimal separator.


If anyone's aware, please let me know. I am in urgent need to use it.
Thanks and Regards,
Ankur- Hide quoted text -

- Show quoted text -


Hi Ben,

I tried your regular expression. It worked for most of the cases. But
in addition to all, it also accepts 1000,000 which it should not have.
Instead 1,000,000 is what should have been allowed. Any minor change
that can be done to the reg-ex you provided??

Secondly, I never posted my efforts on this thread. Here's what I have
been trying my hands on :

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

It's an OR of two reg-ex, first one being the reg-ex to recognise the
number with thousand separator and second without thousand separator.
The pipe "|" doesn't seem to OR the two conditions. Any idea why is
this happening?

Hoping to get some headway..

Regards,
Ankur

Apr 3 '07 #7

P: n/a
Try the following:

\b(?:\d{1,3}(?(?=[,.])[,.](?:\d{3}(?:[.,](?=\d)|\b)|(?:\b|\d{1,2}))|\b))+

It's a bit complicated to explain, but I'll try.

First, it asserts that the match must begin on a word boundary. The match
follows as a non-capturing group which may be repeated any number of times.
The match consists of:

1. 1-3 digits
2. These digits may be followed by a period or comma.
a. If they are followed by a period or a comma, they are followed by
either:
aa. Exactly 3 digits followed by a period or comma that must be
followed by a (not captured) digit, or
bb. a word boundary
b. Otherwise (not followed by a period or comma) either
aa. 1 or 2 digits, or
bb. a word boundary
3. If the initial 1-3 digits are NOT followed by a period or comma, they
must
be followed by a word boundary.

So, essentially, the rules expressed are that a match consists of a sequence
of 1-3 digits with commas between the groups of digits. However, if a group
of digits is surrounded by periods or commas, there must be exactly 3.

The trick is the quantifier on the entire group. This creates a repeating
pattern of groups of 1-3 digits followed by periods or commas. However, the
last group of digits must be followed by a word boundary, and the first
group must be preceded by a word boundary. And if a group of digits is
between periods/commas, there must be exactly 3.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

<he********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
On Mar 30, 8:59 pm, "Ben Voigt" <r...@nospam.nospamwrote:
><hellben...@gmail.comwrote in message

news:11**********************@l77g2000hsb.googleg roups.com...
Hi All,
I need aregularexpressionto be used in an ASP page which would
allow a user to enter:
1000 as well as
1,000
i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)

Something like:

[0-9]+(,[0-9]{3})*

As others have said, this is only good with the (American? Imperial?)
notation using comma for thousands grouping and point as decimal
separator.


If anyone's aware, please let me know. I am in urgent need to use it.
Thanks and Regards,
Ankur- Hide quoted text -

- Show quoted text -

Hi Ben,

I tried your regular expression. It worked for most of the cases. But
in addition to all, it also accepts 1000,000 which it should not have.
Instead 1,000,000 is what should have been allowed. Any minor change
that can be done to the reg-ex you provided??

Secondly, I never posted my efforts on this thread. Here's what I have
been trying my hands on :

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

It's an OR of two reg-ex, first one being the reg-ex to recognise the
number with thousand separator and second without thousand separator.
The pipe "|" doesn't seem to OR the two conditions. Any idea why is
this happening?

Hoping to get some headway..

Regards,
Ankur

Apr 3 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.