Why does one regex and the other not work??

T

tlyczko

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

<html>
<head>
<title>Untitled</title>
</head>
<body>
<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 = '';
}
}
</script>
</head>
<body>
<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" />
</form>
<div id="msg"></div>
</body>
</html>
 
T

tlyczko

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
 
R

robocoder

Your regular expression expects at least 1 digit, i.e., \d{1,5}

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

Michael Winter

On 12/12/2005 01:03, tlyczko wrote:

[snip]
Why does the uncommented RE not work??
[snip]

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.

[snip]

Mike
 
L

Lee

tlyczko said:
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??

Your basic problem is that you're auditing after every keystroke.
You're going to have a lot of trouble writing a RE that is valid
after one keystroke, after the "." has been hit, etc.
Why not just wait until they finish typing and audit onChange?
 
T

tlyczko

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.

Tom
 
L

Lee

tlyczko said:
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.

Feedback while a person is typing doesn't add a lot of value over feedback when
they finish, particularly when the field is only a handful of characters.
They may even be less likely to notice it.

I advice against using onblur. There are too many things that can cause a field
to lose focus before the person has finished typing. If I start to type a
number, then decide to change focus to double-check something, I will be annoyed
if the browser nags me about an invalid entry. Even more so if some alarm
window pops up and takes focus momentarily.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top