Skip to main content
Participant
August 23, 2017
Question

Force Javascript to update

  • August 23, 2017
  • 1 reply
  • 304 views

I have a field that a person enters a value into.  The value has to be entered as a value of 0.5 or 0.  For example:

12.00

12.50

13.00

Those values are acceptable.  A value of 12.25 is not acceptable.  Is there a way to basically not except a value that does not end with a .00 or a 0.50 and just leave the field blank if the operators enters something other then this?

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
August 23, 2017

Yes. Use this code as the field's custom validation script:

if (event.value) event.rc = (Number(event.value)%0.5==0);

Walb0244Author
Participant
August 24, 2017

I am wanting the code to automatically round up to the nearest 0.5

So if the person enters 34.75 then it will round the value up to the nearest 0.5 which would be 35.00

If the person enter 34.25 then it would round up to 35.50

try67
Community Expert
Community Expert
August 25, 2017

This is a purely mathematical task. I've written some sample code that demonstrates how to do it:

var a = 34.25;

var round_a = Math.floor(a);

if (a!=round_a) {

    if ((a-round_a)<=0.5)

        a = round_a+0.5;

    else a = round_a+1;

}

console.println(a);