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>
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>