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

Why does one regex and the other not work??

P: n/a
Here is the code to test. This code was generously provided (some
changes by ME) by RobG from this list.

Rob's RE is the one commented out, to have at least 1 to 5 digits
before the decimal and two digits after the decimal, it allows a
negative number.

Same RE, uncommented, is mine, with an escaped $ (dollar sign) so I
could use it for evaluating currency entries.

Why does the uncommented RE not work??

Thank you, Tom

<script type="text/javascript">
function testNum(num, erId){
var er = document.getElementById(erId);
//var re = /^[-]?\d{1,5}(\.\d{1,2})?$/;
var re = /^[\$]?\d{1,5}(\.\d{1,2})?$/;

if (num && !re.test(num) ) {
er.innerHTML = 'Invalid number, required format is [$]nnnn.nn';
} else {
er.innerHTML = '';
<form action="">
$ <input type="text" name="foo" value="" size="10" maxlength="9"
onkeyup="testNum(this.value, 'msg');"
onkeypress="testNum(this.value, 'msg');" />
<input type="reset" value="clear" />
<div id="msg"></div>

Dec 12 '05 #1
Share this Question
Share on Google+
5 Replies

P: n/a
Quote :
"For example, [akm$] will match any of the characters "a", "k", "m", or
"$"; "$" is usually a metacharacter, but inside a character class it's
stripped of its special nature."

Dec 12 '05 #2

P: n/a
Thank you for the reference...I'm trying to match for $ as a currency
symbol and it doesn't work in the above regex as either [$] OR [\$],
but I will try it again as [$]...

Any other help much appreciated...

Thank you, :) tom

Dec 12 '05 #3

P: n/a
Your regular expression expects at least 1 digit, i.e., \d{1,5}

So, a '$', by itself, fails your validation function.

Dec 12 '05 #4

P: n/a
On 12/12/2005 01:03, tlyczko wrote:

Why does the uncommented RE not work??
var re = /^[\$]?\d{1,5}(\.\d{1,2})?$/;

Care to describe what 'not work' means? What values fail? Apart from the
fact that I wouldn't use a character class (the square brackets), I see
nothing wrong with it, and it behaves as expected here.



Michael Winter
Prefix subject with [News] before replying by e-mail.
Dec 12 '05 #5

P: n/a
Ahhh...Thank you Lee.

I have found/tried some regexes that DO allow me to type the $
character without throwing an error, but I do not know how to modify
those other regexes to ensure that the end user types two digits if
they type a decimal...

So it looks like I either give up giving feedback as the person types
or I use onblur or onchange to evaluate the typed data in the text box.

Can't have everything, I suppose. :)

With regard to robocoder's comments, thank you for explaining. I don't
know how to change the first part of the regex to do what you suggest,
so I guess until I have time to learn more about regexes, I have to
fall back on just evaluating without the $ character.

Thank you, everyone, for your help.


Dec 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.