Print Page | Close Window

Force input whole number

Printed From: Foxit's Planet PDF Forum
Category: Lets Talk PDF
Forum Name: JavaScript
Forum Description: Discussions relating to the use of JavaScript within PDF.
URL: http://forums.planetpdf.com/forum_posts.asp?TID=1670
Printed Date: 21 Oct 2019 at 11:35pm
Software Version: Web Wiz Forums 11.10 - http://www.webwizforums.com


Topic: Force input whole number
Posted By: Maureen
Subject: Force input whole number
Date Posted: 13 Aug 2015 at 3:00pm

Hello all,

As you'll noticed I'm  Javascript newbie.

How can I force that field have to field in with a whole number (no rounding up). If a user wants to fill in a number with a decimal, an allert pops up "Please fill in a whole number)

Somebody wants to help with a javascript to do this?

Thank you



Replies:
Posted By: gkaiseril
Date Posted: 13 Aug 2015 at 6:46pm
JavaScript has the https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor - Math.floor()  function to truncate the value to the next lower whole number.
 
In the custom JavaScript calculation one can input to force the value to the closest next lower integer.
 
event.value = Math.floor(event.value);
 
If you want a custom validaton:
 
if(event.value != Math.floor(event.value) {
app.alert("Please fill in a whole number", 1, 0);
this.resetForm([event.target.name]);
event.rc = false;
}
 
One could also use a script to limit the keystroke input to digits only.

 


Posted By: Maureen
Date Posted: 14 Aug 2015 at 3:51pm
Thks for your reply, but I've used your script in the custom validation, bu no alerts popsup when filling in a value with decimals. I used this script:

if(event.value != Math.floor(event.value) {
app.alert("Please fill in a whole number", 1, 0);
this.resetForm([event.target.name]);
event.rc = false;
}

What have I done wrong?


Posted By: gkaiseril
Date Posted: 14 Aug 2015 at 4:55pm
I get an error. There is a missing parenthesis in the condition statement.
 
if(event.value != Math.floor(event.value)) {
app.alert("Please fill in a whole number", 1, 0);
this.resetForm([event.target.name]);
event.rc = false;
}
 


Posted By: Maureen
Date Posted: 17 Aug 2015 at 10:24am
Thank you. It works!!



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.10 - http://www.webwizforums.com
Copyright ©2001-2017 Web Wiz Ltd. - https://www.webwiz.net