Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2005, 03:17 AM   PM User | #1
jianneng
New Coder

 
Join Date: Aug 2002
Location: Malaysia
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
jianneng is an unknown quantity at this point
Forcing only numbers with .0 or .5 only

Dear all, is there any reference to this javascript that forces a user to enter a number that is either a whole number or only with a .5 decimal? e.g. 1, 2, 3.5, 8.5, 9.0 etc. If the user enters say 8.4, 9.3, 7.7, an error message will appear and clears the current textbox?
jianneng is offline   Reply With Quote
Old 01-27-2005, 07:49 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
if (/\.[12346789]/g.test(thetextbox.value)) {
alert ("Error");
thetextbox.value="";
thetextbox.focus
}
else {

...... OK
}

European users my place a comma , as seperator so better

if (/(\.|\,)[12346789]/g.test(thetextbox.value)) {

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.
Philip M is offline   Reply With Quote
Old 01-28-2005, 07:09 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
In fact on second thoughts this has an imperfection. It will block 6.8 for example but not 6.08.

Another approach is simply using modulo arithmetic:

x = thetextbox.value
if ((x%0.5) !=0 ) {
alert ("Error!");
thetextbox.value="";
thetextbox.focus;
}
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:10 AM.


Advertisement
Log in to turn off these ads.