aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes decimal point restriction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "decimal point restriction" Watch "decimal point restriction" New topic
Author

decimal point restriction

Anurag Mishra
Ranch Hand

Joined: Jun 16, 2003
Posts: 121
Hi All,
I need help its related to somple text filed entry for numbers.
I want user to enter values in form of 22.99 or 0.99 or .88 etc
I want user to restrict entering any value more than 2 decimal point.
It means user should allow to enter anything after .89
pls reply to that.
thanks
Anurag
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Here is the basic idea of what you are after.
Anurag Mishra
Ranch Hand

Joined: Jun 16, 2003
Posts: 121
Hi Thanks for Reply,
I got the way to do this which is simpler I am posting java script here so if anyone else require this can use easily.
//this function will restrict user to enter values upto two decimal point only
function validDecimal(str){
if (str.indexOf('.') == -1) str += ".";
var decNum = str.substring(str.indexOf('.')+1, str.length);
if (decNum.length > 2)//here is the key u can just change from 2 to 3,45 etc to restict no of digits aftre decimal
{
alert("Invalid more than 2 digits after decimal")
}
else {
alert("Valid no")
}
}
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Mine actually goes a step further by making sure that numbers are entered and that nothing else is entered into the text box.
Eric
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
In keeping with the mantra "it's better to prevent the user from making a mistake than to correct him after", here is my solution. It only allows the user to enter numbers in the field, and only allows two numbers after the decimal place.


Tom Blough


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Tom, One action will blow your script to pieces and that is pasting...
Anurag Mishra
Ranch Hand

Joined: Jun 16, 2003
Posts: 121
Hi All,
Its great you all replied, I just wanted to put script here so any one coming to javaranch can get help.
Eric ur right I haven't shown number validation here I am assuming its done already.
thanks
Anurag
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Originally posted by Eric Pascarello:
Tom, One action will blow your script to pieces and that is pasting...

You're right Eric. You'd have to add some onChange checking as well.
Tom
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Tom,
all you need to do is do an onchange handler,
you can check it with the regular expression I have up top, if it matches then allow it, if not then clear it and force them to type it.
Eric
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Originally posted by Eric Pascarello:
Tom,
all you need to do is do an onchange handler,
you can check it with the regular expression I have up top, if it matches then allow it, if not then clear it and force them to type it.
Eric

You miss my point, Eric. The goal is not to wait until after the user types "sdf2134SDR2q3asdf.2@#$dd3w32" and then do some action that lets him know it is invalid.
The goal is to restrict the vaild inputs to the field to just what you want. onKey will prevent invalid characters from even being entered so the user gets instant feedback, and onChange will prevent pasting invalid text - the user still receives instant feedback in this case as well.
Tom
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Tom,
I am not sure if you understood what I meant...
I was just saying how you can get around the pasting part of the code, your method would still be enforced with typing the code if the person types the entry.
So if I come and paste <b>3.21</b> your script would ignore it.
If I pasted <b>fdgdfghdfjghkhdfgjkhfd</b> You script would delete the crap and force the person to type it in.
Eric
 
wood burning stoves
 
subject: decimal point restriction