How to ensure that negative number can't be entered for text entry? | XM Community
Solved

How to ensure that negative number can't be entered for text entry?

  • 29 April 2020
  • 5 replies
  • 243 views

Badge

I saw on this thread how to ensure that only numbers can be entered for text entry:
How to set a field to number only?I specifically used this solution to make it so respondents can only enter integers between 0 and 9:
jQuery("#"+this.questionId+" .InputText").attr({'type':'number','min':'0', 'max':'9'});
However, I've noticed that a minus sign can still be entered (e.g., "- 5"). Is there some way to prevent this possibility?

icon

Best answer by rondev 29 April 2020, 22:39

View original

5 replies

Userlevel 7
Badge +22

No need of custom code here, as this feature is present in qualtrics. In text entry question, select validation type as "Content validation", content type as "Number" and under numeric format put 0 as min and 9 as max

Userlevel 3
Badge +2

how about using "pattern":"[0-9]{1}"?
: The Input (Form Input) element

Badge

How to ensure that negative number can't be entered for text entry?This doesn't quite work because this problem is embedded within a trickier problem where I want to display 6 entry forms side-by-side for text entry. This code gets be very close except for being able enter minus signs:

Qualtrics.SurveyEngine.addOnReady(function()

{

/*Place your JavaScript here to run when the page is fully displayed*/



var that = this.questionId;

  jQuery("#"+this.questionId+" td:not(.ControlContainer)").hide();

jQuery("#"+this.questionId+" .InputText").css("max-width","2em");

jQuery("#"+this.questionId+" .InputText").attr("maxlength", "1");

  jQuery("$").insertBefore(jQuery("#"+that+" .InputText:eq(0)"));

  jQuery("#"+that+" .InputText:eq(1)").insertAfter(jQuery("#"+that+" .InputText:eq(0)"));

jQuery("#"+that+" .InputText:eq(2)").insertAfter(jQuery("#"+that+" .InputText:eq(1)"));

jQuery("#"+that+" .InputText:eq(3)").insertAfter(jQuery("#"+that+" .InputText:eq(2)"));

jQuery("#"+that+" .InputText:eq(4)").insertAfter(jQuery("#"+that+" .InputText:eq(3)"));

jQuery("#"+that+" .InputText:eq(5)").insertAfter(jQuery("#"+that+" .InputText:eq(4)"));

  jQuery(",").insertAfter(jQuery("#"+that+" .InputText:eq(2)"));

jQuery("#"+this.questionId+" .InputText").attr({'type':'number','min':'0', 'max':'9'});


Badge

How to ensure that negative number can't be entered for text entry?This didn't work either. I tried this code and was able to input letters etc.
jQuery("#"+this.questionId+" .InputText").attr({"pattern":"[0-9]{1}"});

Userlevel 7
Badge +22

Check this post

Leave a Reply