Checking for a decimal

M

Mark

Hi - can anyone give me a sample of how I would ensure a value in a text
box was a decimal (it is used for entering money - so has to check for
no more than 2 decimal places, but still allow should no decimals be
input) - eg.

23 - ok
23.01 - ok
23.1 - ok
23.111 - not valid

Thanks for any help,

Mark


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
F

Flurk

Mark said:
Hi - can anyone give me a sample of how I would ensure a value in a
text box was a decimal (it is used for entering money - so has to
check for no more than 2 decimal places, but still allow should no
decimals be input) - eg.

23 - ok
23.01 - ok
23.1 - ok
23.111 - not valid

Use the regular expression ^(\d+\.\d{0,2}|\d+)$

This is how you validate your form:

<form onsubmit="return /^(\d+\.\d{0,2}|\d+)$/.test(this.mynumber.value);">
<input type="text" name="mynumber">
<input type="submit">
</form>
 
L

Lasse Reichstein Nielsen

Mark said:
Hi - can anyone give me a sample of how I would ensure a value in a text
box was a decimal (it is used for entering money - so has to check for
no more than 2 decimal places, but still allow should no decimals be
input) - eg.

23 - ok
23.01 - ok
23.1 - ok
23.111 - not valid

Is "23." valid? I guess not.

A simple method that tests for the correct number of digits:

function(value) {
return /^\d+(\.\d{1,2})?$/.exec(value);
}

What do you want to do if the test fails?


You can also use the more heavyhanded approach of enforcing the format
whenever the user changes the content:

<input type="text" ...
onchange="var match = /^([-+]?\d+)(\.\d*)?$/.exec(this.value);
if (!match) {
this.value = ''; // whatever input was completely wrong.
} else if (match[2]) {
var decimals = match[2].substring(0,3);
decimals += '00'.substring(decimals.length-1);
this.value = match[1]+decimals;
}">

Completely removing the content on a non-number input might be a little
too rough. It allows prefixed signs, unlike the first test. (Should it?)
It truncates extra digits, it doesn't round.

Good luck
/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Hi - can anyone give me a sample of how I would ensure a value in a text
box was a decimal (it is used for entering money - so has to check for
no more than 2 decimal places, but still allow should no decimals be
input) - eg.

23 - ok
23.01 - ok
23.1 - ok
23.111 - not valid

Thanks for any help,

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

How do you propose, then, to reward us for our assistance?

The third case should NOT be allowed, since it could be intended to mean
either 23.01 or 23.10 - proper money format, in most countries, is an
integer optionally followed by (point digit digit).

The point is adequately explained in p.19 of the free booklet "your
guide to decimal money" - in which Lord Fiske asserts that copies are
being delivered to every household - published by HMSO about 1970.

The basic RegExp is of the form /^\d+(\.\d\d)?$/

See also <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
 
M

Mick White

Flurk said:
Use the regular expression ^(\d+\.\d{0,2}|\d+)$

This is how you validate your form:

<form onsubmit="return /^(\d+\.\d{0,2}|\d+)$/.test(this.mynumber.value);">
<input type="text" name="mynumber">
<input type="submit">
</form>


That would validate "23."
Mick
 

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