473,320 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Checking for a decimal


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!
Jul 23 '05 #1
4 5410
Mark wrote:
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>
--

Flurk
Jul 23 '05 #2
Mark <an*******@devdex.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3
Flurk wrote:

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
Jul 23 '05 #4
JRS: In article <40**********************@news.newsgroups.ws>, seen in
news:comp.lang.javascript, Mark <an*******@devdex.com> posted at Sat, 12
Jun 2004 09:01:34 :

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

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Dave Robinson | last post by:
I was wondering if anyone could help me with a problem I'm having. I've been using Dreamweaver to create a hotel booking system for a friend of mine, using MySQL (version 4.0.21) and PHP 5. The...
1
by: Ramzi Abboud | last post by:
Is there a way to check the type of a variable in SQL? I want to know if a variable is a float or a string. Thanks, Ramzi
5
by: William Payne | last post by:
Hello, I am in the process of converting a C++ program to a C program. The user of the program is supposed to supply an integer on the command line and in the C++ version of the program I was using...
5
by: Playa | last post by:
I have a textbox and I only want the user to enter numbers. When the user clicks the calculate event I want to check to make sure only numbers are entered and if there is characters i want to...
5
by: dav.phillips | last post by:
Hi, I am very new to all this and need to know how to check a variable to see if it is a number or not. Also can anyone recommend a reference book apart from dive into python preferably a...
10
by: mcbobin | last post by:
Hi, Here's hoping someone can help... I'm using a stored procedure to return a single row of data ie a DataRow e.g. public static DataRow GetManualDailySplits(string prmLocationID, string
7
by: Jay | last post by:
I want to check if a character is a decimal digit (0-9). I've found Char.IsNumber() and Char.IsDigit() but couldn't work out from Help how they differ. Also, do I need to worry about how digits...
4
by: barmatt80 | last post by:
I am stumped on the error reporting with sql server. I was told i need to return @SQLCode(code showing if successful or not) and @ErrMsg(and the message returned). I am clueless on this. I...
7
by: elnoire | last post by:
Greetings! I've just started learning python, so this is probably one of those obvious questions newbies ask. Is there any way in python to check if a text file is blank? What I've tried to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.