Welcome to the LimeSurvey Community Forum

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

Allow only numbers in the second column of a text array question

  • Vinicos
  • Vinicos's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
3 years 8 months ago #204085 by Vinicos
Hello,

I have created a text array question with two columns. I'm asking participants to list goals on the first column and to ascribe the age he/she thinks will have when achieving this goal.
I have already created a subquestion validation equation like that:

((((is_numeric(LPSgoals_goal1_age)) and (LPSgoals_goal1_age > (Age - 1))) or (is_empty(LPSgoals_goal1_age))) and (((is_numeric(LPSgoals_goal2_age)) and (LPSgoals_goal2_age > (Age - 1))) or (is_empty(LPSgoals_goal2_age))) and (((is_numeric(LPSgoals_goal3_age)) .....

But, still, participants are sometimes using letters on the second column (e.g.: 37 years old). That is leading to a lot of missing data.

I want the second column to allow only numeric characters. I mean, if participants try to write a letter, the field won't allow. How can I do that?

LS 3.22

File Attachment:

File Name: limesurvey...1765.lsq
File Size:69 KB
The topic has been locked.
More
3 years 8 months ago #204087 by jelo
You might attach a LSS export. Lsq is a lot harder to work with. LSQ can only be imported in a survey with the correct language setting. And codes can be changed when importing to a survey. LSS exports can be imported directly and all get the same codes.

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #204090 by tpartner

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 #204130 by Vinicos
Sorry.
I read the post three times, but I really couldn't understand which code to use. The person who started the question was putting a harder issue, as her question contained a dropdown column and a numeric one. They changed a lot of codes and some of them were pretty long. Programing language is something really hard for me to grasp. My question seems to be simpler: I just want the second column to allow only numbers.

I thank you guys on trying to help me, but I have some limitation
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #204137 by tpartner
Did you try the sample survey provided there?

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 #204157 by Vinicos
Let me explain what I did:

I had this code in this question (due to other reasons):

<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:complete',function() {

// Identify this question
var thisQuestion = $('#question{QID}');

// First column
$('td.answer-item:nth-child(2) input:text', thisQuestion).attr('size', 200);
// Second column
$('td.answer-item:nth-child(3) input:text', thisQuestion).attr('size', 4);
});
</script>

I added the code:

if($.isNumeric(thisValue) === false) {
// Strip out non-numerics characters
newValue = thisValue.replace(/\D/g,'');
$(this).val(newValue).trigger('change');
}

After

$('td.answer-item:nth-child(3) input:text', thisQuestion).attr('size', 4);

But it didn't work :(

What is wrong?

Sorry if the question is stupid
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #204171 by tpartner
You don't have a listener event.

Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).on('ready pjax:scriptcomplete',function(){
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Assign column-specific classes
    $('tr.subquestion-list', thisQuestion).each(function(i) {
      $('th, td', this).each(function(i) {
        $(this).addClass('column-'+i);
      });
    });
 
    // First column
    $('.answer-item.column-1  input:text', thisQuestion).attr('size', 200);
    // Second column
    $('.answer-item.column-2 input:text', thisQuestion).attr('size', 4);
 
    // Listener on column 2 inputs
    $('.answer-item.column-2 input:text', thisQuestion).on('keyup change', function(e) {
      var thisValue = $.trim($(this).val());
 
      // Strip out non-numerics characters
      newValue = thisValue.replace(/\D/g,'').replace(/,/g,'').replace(/\./g,'');
      $(this).val(newValue).trigger('change');
    });
  });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...2(1).lss
File Size:31 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 7 months ago #204335 by Vinicos
Thanks. It's working perfectly now.
Something occurred to me now. And what if I wanted to allow maximum 3 characters? What should I add to the code?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 7 months ago - 3 years 7 months ago #204338 by tpartner
Code:
// Second column
$('.answer-item.column-2 input:text', thisQuestion).attr('maxlength', 3);

- www.w3schools.com/tags/att_input_maxlength.asp

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 3 years 7 months ago by tpartner.
The topic has been locked.
  • Vinicos
  • Vinicos's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
3 years 7 months ago #204340 by Vinicos
Should I add this code after:

// Strip out non-numerics characters
newValue = thisValue.replace(/\D/g,'').replace(/,/g,'').replace(/\./g,'');
$(this).val(newValue).trigger('change');

?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 7 months ago #204345 by tpartner
After:

Code:
$('.answer-item.column-1  input:text', thisQuestion).attr('size', 200);

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 7 months ago #204356 by Vinicos
Thanks
It's working well
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose