• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

decimal point restriction

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the basic idea of what you are after.
 
Anurag Mishra
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mine actually goes a step further by making sure that numbers are entered and that nothing else is entered into the text box.
Eric
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tom, One action will blow your script to pieces and that is pasting...
 
Anurag Mishra
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
I'm so happy! And I wish to make this tiny ad happy too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic