Form Field - Character Limit | XM Community
Solved

Form Field - Character Limit

  • 25 March 2024
  • 5 replies
  • 32 views

Userlevel 6
Badge +5

Hi Everyone

I am using this code successfully in an essay text box.

Qualtrics.SurveyEngine.addOnload(function()
{

jQuery("#"+this.questionId+" .InputText").attr("placeholder", "500 character limit.");
jQuery("#"+this.questionId+"-RemainingChars").css({"color":"#8c8c8c","font-size":"15px","visibility":"collapse"});
jQuery("#"+this.questionId+"-RemainingChars span").css({"visibility":"visible","float":"left"});

Which gives this me this.

How can I do the exact same but for a form field question with x2 fields?

 

Thanks :)

icon

Best answer by Deepak 25 March 2024, 19:18

View original

5 replies

Userlevel 7
Badge +36

Hi @parkie_0007 ,

Try below code, it should work for form field.
 

Qualtrics.SurveyEngine.addOnload(function() {
var maxChars = 500;
jQuery("#" + this.questionId + " .InputText").attr("placeholder", "500 character limit.").each(function() {
var input = jQuery(this), display = jQuery("<div style='font-size:0.8em;'>Characters remaining: <span class='charCount'>" + maxChars + "</span></div>").insertAfter(input);
input.on("input", function() {
var remainingChars = maxChars - this.value.length;
if (remainingChars < 0) {
this.value = this.value.substr(0, maxChars);
remainingChars = 0;
}
display.find(".charCount").text(remainingChars);
});
});
});

 

Userlevel 6
Badge +5

Thanks @Deepak !

Really helpful!

  • How would I make the colour 8c8c8c?
  • How would I make the font-size 15px?
  • How would I show the remaining characters only after the 1st character is inputted.

My form question has x2 fields.

  • How would I make this applicable just for the 1st field?

Thanks! 🙏🏻

Userlevel 7
Badge +36

@parkie_0007 

Qualtrics.SurveyEngine.addOnload(function() {
var maxChars = 500;
jQuery("#" + this.questionId + " .InputText").eq(0).attr("placeholder", "500 character limit.").each(function() {
var input = jQuery(this),
display = jQuery("<div style='font-size:15px; color:#8c8c8c; visibility:hidden;'>Characters remaining: <span class='charCount'>" + maxChars + "</span></div>").insertAfter(input);

input.on("input", function() {
var remainingChars = maxChars - this.value.length;
if (remainingChars < 0) {
this.value = this.value.substr(0, maxChars);
remainingChars = 0;
}

if (this.value.length > 0) {
display.css('visibility', 'visible');
} else {
display.css('visibility', 'hidden');
}

display.find(".charCount").text(remainingChars);
});
});
});

 

Userlevel 6
Badge +5

Amazing, thanks :)

Userlevel 6
Badge +5

Sorry! @Deepak !

What if I wanted this again for the second field in the form?

Thanks :)

Leave a Reply