We are asking a lot of questions which should be only answered by numbers. How is it possible to restrict a survey field only to numbers? Does this automatically result in the "number keyboard" showing up directly when the survey is used on a mobile phone?
Thanks
Topher
Page 1 / 1
You can change a text input field to an html5 number input field using JavaScript. For example:
```
jQuery("#"+this.questionId+" .InputText").attr('type', 'number');
```
Mobile devices will automatically pop up the numeric keyboard when a number field gets focus.
Depending on your need, you may need to add additional attributes like step, min, etc. You should still use Qualtrics Content Validation - Content Type - Number.
```
jQuery("#"+this.questionId+" .InputText").attr('type', 'number');
```
Mobile devices will automatically pop up the numeric keyboard when a number field gets focus.
Depending on your need, you may need to add additional attributes like step, min, etc. You should still use Qualtrics Content Validation - Content Type - Number.
Thank you! This is helpful!
How do I enter the javascript?
How do I enter the javascript?
Click on the cog next to the question:
!
Add the line of JavaScript to the addOnload function. It is only an example for a simple text entry question. You may need to modify it based on your question. As I wrote earlier, you may also need to add additional attributes.
!
Add the line of JavaScript to the addOnload function. It is only an example for a simple text entry question. You may need to modify it based on your question. As I wrote earlier, you may also need to add additional attributes.
Hi
I'm also trying to set multiple fields to 'number only'. The Java script works very well. However, I noticed that it is still possible to type the letter 'e'. Would there be a possiblity to change that?
Thanks
Anneleen
I'm also trying to set multiple fields to 'number only'. The Java script works very well. However, I noticed that it is still possible to type the letter 'e'. Would there be a possiblity to change that?
Thanks
Anneleen
> @Anneleen said:
> Hi
> I'm also trying to set multiple fields to 'number only'. The Java script works very well. However, I noticed that it is still possible to type the letter 'e'. Would there be a possiblity to change that?
> Thanks
> Anneleen
@Anneleen ,
Paste the following code in js(OnReady) of your question.
jQuery("#"+this.questionId+" .InputText").attr('type', 'number');
document.querySelector(".InputText").addEventListener("keypress", function (evt) {
if (evt.which != 8 && evt.which != 0 && evt.which < 48 || evt.which > 57)
{
evt.preventDefault();
}
});
> Hi
> I'm also trying to set multiple fields to 'number only'. The Java script works very well. However, I noticed that it is still possible to type the letter 'e'. Would there be a possiblity to change that?
> Thanks
> Anneleen
@Anneleen ,
Paste the following code in js(OnReady) of your question.
jQuery("#"+this.questionId+" .InputText").attr('type', 'number');
document.querySelector(".InputText").addEventListener("keypress", function (evt) {
if (evt.which != 8 && evt.which != 0 && evt.which < 48 || evt.which > 57)
{
evt.preventDefault();
}
});
> @Anneleen ,
>
> Paste the following code in js(OnReady) of your question.
>
> jQuery("#"+this.questionId+" .InputText").attr('type', 'number');
>
> document.querySelector(".InputText").addEventListener("keypress", function (evt) {
> if (evt.which != 8 && evt.which != 0 && evt.which < 48 || evt.which > 57)
> {
> evt.preventDefault();
> }
> });
>
>
Hello @Anneleen,
The code of @LaT , will work for you but it will also prevent "+ and -", if you just want to prevent "e" from input than use following code:
jQuery("#"+this.questionId+" .InputText").attr('type', 'number');
document.querySelector(".InputText").addEventListener("keypress", function (evt) {
console.log(evt.which);
if (evt.which ==101 || evt.which ==69)
{
evt.preventDefault();
}
});
>
> Paste the following code in js(OnReady) of your question.
>
> jQuery("#"+this.questionId+" .InputText").attr('type', 'number');
>
> document.querySelector(".InputText").addEventListener("keypress", function (evt) {
> if (evt.which != 8 && evt.which != 0 && evt.which < 48 || evt.which > 57)
> {
> evt.preventDefault();
> }
> });
>
>
Hello @Anneleen,
The code of @LaT , will work for you but it will also prevent "+ and -", if you just want to prevent "e" from input than use following code:
jQuery("#"+this.questionId+" .InputText").attr('type', 'number');
document.querySelector(".InputText").addEventListener("keypress", function (evt) {
console.log(evt.which);
if (evt.which ==101 || evt.which ==69)
{
evt.preventDefault();
}
});
@Anneleen,
'e' is there to allow for scientific notation. If you don't want to allow 'e', you can update the JS to remove it as soon as it is typed.
```
var input = jQuery("#"+this.questionId+" .InputText");
input.attr('type', 'number');
input.on('input', function() { this.value = this.value.replace(/e/g, ''); });
```
'e' is there to allow for scientific notation. If you don't want to allow 'e', you can update the JS to remove it as soon as it is typed.
```
var input = jQuery("#"+this.questionId+" .InputText");
input.attr('type', 'number');
input.on('input', function() { this.value = this.value.replace(/e/g, ''); });
```
Is it possible to modify this line of code
`jQuery("#"+this.questionId+" .InputText").attr('type', 'number');`
so that it only allows positive integers? This would mean that if the number in the box is zero, the "arrow down" that replaces 0 with -1 disappears.
`jQuery("#"+this.questionId+" .InputText").attr('type', 'number');`
so that it only allows positive integers? This would mean that if the number in the box is zero, the "arrow down" that replaces 0 with -1 disappears.
> @DanielE said:
> Is it possible to modify this line of code
> `jQuery("#"+this.questionId+" .InputText").attr('type', 'number');`
> so that it only allows positive integers? This would mean that if the number in the box is zero, the "arrow down" that replaces 0 with -1 disappears.
Yes:
```
jQuery("#"+this.questionId+" .InputText").attr({'type':'number','min':'0'});
```
> Is it possible to modify this line of code
> `jQuery("#"+this.questionId+" .InputText").attr('type', 'number');`
> so that it only allows positive integers? This would mean that if the number in the box is zero, the "arrow down" that replaces 0 with -1 disappears.
Yes:
```
jQuery("#"+this.questionId+" .InputText").attr({'type':'number','min':'0'});
```
I posted a question on this earlier but have since had it sorted - not sure how to delete so just editing the comment!
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.