Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Allow positive numbers only

Questions related to using nuBuilder Forte.
Post Reply
Martin
Posts: 30
Joined: Fri Nov 09, 2018 5:42 pm

Allow positive numbers only

Unread post 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?
Janusz
nuBuilder Team
Posts: 508
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 10 times
Been thanked: 18 times

Re: Allow positive numbers only

Unread post 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()));
You do not have the required permissions to view the files attached to this post.
If you like nuBuilder, please leave a review on SourceForge
admin
Site Admin
Posts: 2822
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 29 times

Re: Allow positive numbers only

Unread post 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
kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: Allow positive numbers only

Unread post 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
You do not have the required permissions to view the files attached to this post.
Martin
Posts: 30
Joined: Fri Nov 09, 2018 5:42 pm

Re: Allow positive numbers only

Unread post by Martin »

@Janusz: Thanks! However, in my case, I need decimals.

@Steven & Kevin: I'll take a look at it. Thanks!
Post Reply