Welcome to the nuBuilder Forums!

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

Limit Input to Only Numeric Topic is solved

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
pmjd
Posts: 132
Joined: Fri Mar 12, 2021 10:38 am
Has thanked: 3 times
Been thanked: 1 time

Limit Input to Only Numeric

Unread post by pmjd »

Hello,

I've been using the new attributes maxlength option to limit the number of characters that can be inputted to a text object, and it works well.
I'd also like to limit it to only accepting numbers and backspace.

I've tried using the code at https://www.dofactory.com/html/input-at ... idyouknow2 by adding the following to the onkeydown event (both with and without the original double quotes and also encasing it within a function)

Code: Select all

return (event.charCode != 8 && event.charCode == 0 || (event.charCode >= 48 && event.charCode <= 57))
but it doesn't seem to work.

Can anyone suggest a way to get it to work?

Thanks,
Paul
kev1n
nuBuilder Team
Posts: 4304
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Limit Input to Only Numeric

Unread post by kev1n »

Hi Paul,

Can't you use a Number Input Type?
pmjd
Posts: 132
Joined: Fri Mar 12, 2021 10:38 am
Has thanked: 3 times
Been thanked: 1 time

Re: Limit Input to Only Numeric

Unread post by pmjd »

Unfortunately the number type doesn't stop any non-numeric input, it allows it and then defaults to 0 after saving.

Also the number input type does not allow the maxlength attribute to function. Maxlength is only supported in password, search, tel, text, and url input types.
https://developer.mozilla.org/en-US/doc ... -maxlength
kev1n
nuBuilder Team
Posts: 4304
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Limit Input to Only Numeric

Unread post by kev1n »

Otherwise, try:

Code: Select all

var charCode = (event.which) ? event.which : event.keyCode
return !(charCode < 48 || charCode > 57) || charCode == 8
pmjd
Posts: 132
Joined: Fri Mar 12, 2021 10:38 am
Has thanked: 3 times
Been thanked: 1 time

Re: Limit Input to Only Numeric

Unread post by pmjd »

Great, your code works. Thank you very much :)
Post Reply