Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Only one character allowed

  • Vinicos
  • Vinicos's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
3 years 8 months ago #203768 by Vinicos
Only one character allowed was created by Vinicos
Hello

I have created a multiple short text question with 3 subquestions. I want the first and the third subquestion to allow only one-character answers. I have already used a subquestion validation equation, but I actually want that the field accepts only one character (just like when we set a maximum of character in a numeric question). Is this possible?

Thanks

LS 3.22
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #203771 by tpartner
Replied by tpartner on topic Only one character allowed
Place this script in the question source:

Code:
<script type="text/javascript" charset="utf-8">  
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // First input
    $('#question{QID} input:text:eq(0)').attr('maxlength', 1);
    // Third input
    $('#question{QID} input:text:eq(2)').attr('maxlength', 1);
    });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...6(1).lss
File Size:23 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Vinicos
  • Vinicos's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
3 years 8 months ago #203772 by Vinicos
Replied by Vinicos on topic Only one character allowed
Thanks a lot.
It works well.
Is it also possible to allow participants only to use letters (not as a question validation equation)?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #203774 by tpartner
Replied by tpartner on topic Only one character allowed
This will also remove all non-alpha characters:

Code:
<script type="text/javascript" charset="utf-8">  
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // First input
    $('#question{QID} input:text:eq(0)').attr('maxlength', 1);
    // Third input
    $('#question{QID} input:text:eq(2)').attr('maxlength', 1);
 
    // Listener on inputs
    $('#question{QID} input:text:eq(0), #question{QID} input:text:eq(2)').on('keyup', function(e) {
      var thisValue = $.trim($(this).val());
 
      // Strip out non-alpha characters
      newValue = thisValue.replace(/\W/g,'').replace(/\d/g,'');
      $(this).val(newValue).trigger('change');
    });
    });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...6(3).lss
File Size:24 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Vinicos
  • Vinicos's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
3 years 8 months ago #203775 by Vinicos
Replied by Vinicos on topic Only one character allowed
Sorry to bother again.
But, in this case, I would like the three subquestions to allow only letters. The first and the third only one character, but the second as much as the participants want (but only letters). Would the syntax be different then?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago - 3 years 8 months ago #203776 by tpartner
Replied by tpartner on topic Only one character allowed
Change:

Code:
$('#question{QID} input:text:eq(0), #question{QID} input:text:eq(2)').on('keyup', function(e) {

To:

Code:
$('#question{QID} input:text').on('keyup', function(e) {

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 3 years 8 months ago by tpartner.
The topic has been locked.
  • Vinicos
  • Vinicos's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
3 years 8 months ago #203829 by Vinicos
Replied by Vinicos on topic Only one character allowed
Thanks. It works perfectly now.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose