Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

typing only numeric characters

How can I force the input box to type only numbers?
asked Oct 11, 2021 by Ahmed Anwer Bronze (995 points)
Thank you Zachary, can I add this code to a other specify input box that I use for numeric entry?

2 Answers

+1 vote

Please try adding this script to your numeric question:

$(document).on('keydown', '#[% QuestionName() %]', function(event){
    var key = event.which;
    if (key < 48 || key > 57) {
        return false;

Note that this solution currently prohibits decimals as well as copy-and-pasted responses.  If either of those are issues, we can look into possible solutions.
answered Oct 11, 2021 by Zachary Platinum Sawtooth Software, Inc. (212,550 points)
it's working on numeric questions, but it's not working on an other input box like this Q1_1_other. how can I do that?
You could replace this:

#[% QuestionName() %]

with this for your other specify field:

#[% QuestionName() %]_1_other

or something like this to apply to all inputs on the page:

input, textarea

It really just depends on which field(s) you want this to apply to and which you do not.
0 votes
My first question is can't you simply use a numeric question type? That will certainly restrict the input to numbers only.

If you have an O/E question or an other specify comment box to deal with, check out this forum post which should assist you ...

answered Oct 11, 2021 by Paul Moon Platinum (100,795 points)
I want to prevent typing in alphabetic in the first place, like this: https://www.w3schools.com/tags/att_input_type_number.asp
The information I provided earlier are verification solutions. Even the numeric question type allows letters to be entered.

They do not prevent letters or special characters from being entered, they simply confirm if the entry is valid or not.

I'll request Zachary to take a look at your request. He may know of a prevention measure?

Stay tuned.
Thank you so much Paul, it's easy to validate each numeric entry with "isNaN" but I need more of a user-friendly approach
Let's see what Zachary has to say. He has a big bag of tricks.
I did the trick by changing the input type attribute to "Number" instead of "Text" in the browser console: https://i.ibb.co/nzL397v/Capture.png

but I need to change it in the project file as well
Than you Zachary, can I add this code to a other input box that I use for numeric entry?
This should work across any of your other questions where you need to apply this behaviour. The script should need no editing.

And thank you Zachary for chipping in as always my friend. Much appreciated.