Page 1 of 1
Allow positive numbers only
Posted: Tue Apr 14, 2020 5:23 pm
by Martin
Hi,
I've got a form where the users are putting in hours (Number object) and I'd like to make it so that they are only allowed to put in positive numbers. Is there a way to do this?
Re: Allow positive numbers only
Posted: Tue Apr 14, 2020 8:21 pm
by Janusz
Hi,
To allow only positive values to be used like hours for example you can use select field if the range is reasonable short.
Up to one month per person it's quite easy.
For example in one application I use 2 select fields - one for hours and one for minutes with the 5 minutes step
Przechwytywanie.JPG
in the select object-> select tab you can place for hours:
SELECT seq,seq FROM seq_0_to_200;
and for minutes:
SELECT seq,seq FROM seq_0_to_55_step_5;
Other possibility is to check the input field value with the onblur event and automatically adjust.
for example to replace negative values with positive place the following with onblur event:
var field=this.id; $('#'+field).val(Math.abs($('#'+field).val()));
Re: Allow positive numbers only
Posted: Wed Apr 15, 2020 1:07 am
by admin
Martin,
You could put this code on the
onchange event of the number field you want to validate...
Code: Select all
if(Number(this.value) < 0){alert('Must be a positive number...'); this.focus();}
Steven
Re: Allow positive numbers only
Posted: Wed Apr 15, 2020 7:54 am
by kev1n
You could use the HTML <input> min Attribute as described here:
https://www.w3schools.com/tags/att_input_min.asp
To add this attribute to a nuBuilder object, add this JavaScript Code
to your form's Custom Code field .
Code: Select all
if(nuFormType() == 'edit') {
$('#your_object_id').attr('min','0').attr( "step", "any" );
}
Simply replace the
your_object_id with your nuBuilder
object ID.
In addition, add a oninput event to your object's
Custom Code:
oninput.png
Re: Allow positive numbers only
Posted: Wed Apr 15, 2020 10:53 am
by Martin
@Janusz: Thanks! However, in my case, I need decimals.
@Steven & Kevin: I'll take a look at it. Thanks!