Welcome to the LimeSurvey Community Forum

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

Replacing numbers in Array (Texts) with thousands format

  • Baarceri_122875
  • Baarceri_122875's Avatar Topic Author
  • Offline
  • Banned
  • Banned
More
6 years 1 week ago - 6 years 1 week ago #165755 by Baarceri_122875
Hi there,
I am trying to amend some javascript to replace numbers entered in an array (texts) question with the same numbers but formatted to include commas for thousands. I have taken code from here for referencing the question answer, and here for replacing the entry with the same number formatted. Unfortunately, I can't get it to produce the desired result. Any ideas? My code looks like this...
Code:
<script type='text/javascript' charset='utf-8'>
$( document ).ready( function() {
  var this_surveyId='{SID}';
  var this_groupId='{self.gid}';
  var this_quesitonId='{self.qid}';
  var this_self='{self.sgqa}';
 
  var my_targetTextBox='#answer' + this_self;
 
  // Operate for its intrinsic value:
  $( my_targetTextBox ).blur( function() {
    $( this ).val( $( this ).val().replace(/\B(?=(\d{3})+(?!\d))/g, ",") );
  });
});
</script>


Regards,
Will
Last edit: 6 years 1 week ago by Baarceri_122875. Reason: Typos
The topic has been locked.
  • c_schmitz
  • c_schmitz's Avatar
  • Offline
  • LimeSurvey GmbH Employee
  • LimeSurvey GmbH Employee
More
6 years 1 week ago #165756 by c_schmitz
This is a very hard thing to do. We tried this to make this work in LimeSurvey as a core function but gave up on it.

The main problem is that there is no way to correctly format the number while typing, especially if you have decimals, because it would require that you know the full number in advance.

My advice: Don't waste your time on that one - you will regret it.

Best regards

Carsten Schmitz
LimeSurvey project leader
The following user(s) said Thank You: Baarceri_122875
The topic has been locked.
  • Baarceri_122875
  • Baarceri_122875's Avatar Topic Author
  • Offline
  • Banned
  • Banned
More
6 years 1 week ago #165757 by Baarceri_122875
Replied by Baarceri_122875 on topic Replacing numbers in Array (Texts) with thousands format
Ah, ok, thanks anyway!
I thought maybe it would work because the uppercase transforming code in the first link works a treat.

Will
The topic has been locked.
  • markusfluer
  • markusfluer's Avatar
  • Visitor
  • Visitor
6 years 1 week ago #165759 by markusfluer
If you are really keen on getting this to work, this is how it could be working:
Code:
<script type='text/javascript' charset='utf-8'>
 
$( document ).on('ready pjax:scriptcomplete', function() {
  $('#question{self.qid}').find('.answer-item input[type=text]').on('blur', function(){
    var oldVal = $( this ).val();
        var newVal = String(oldVal).replace(/,/g,'').replace(/(\B)(?=(\d\{3\})+(?!\d))/g, ",");
      $( this ).val( newVal );
  });
});
</script>
The topic has been locked.
  • Baarceri_122875
  • Baarceri_122875's Avatar Topic Author
  • Offline
  • Banned
  • Banned
More
6 years 1 week ago #165761 by Baarceri_122875
Replied by Baarceri_122875 on topic Replacing numbers in Array (Texts) with thousands format
Excellent, that works perfectly :)

Thanks,
Will
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 1 week ago - 6 years 1 week ago #165764 by tpartner
You could place a mask on the input. I like RobinHerbots' Inputmask plugin - github.com/RobinHerbots/Inputmask

There are some implementation tips here - www.limesurvey.org/forum/can-i-do-this-w...-masket-input#164412

I think, if using Inputmask, you would need something like this in the question source:

Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){ 
    $('#question{QID} input[type="text"]').inputmask({
      'alias': 'decimal',
      'rightAlign': false,
      'groupSeparator': '.',
      'autoGroup': true,
      'digits': 2
    });
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 6 years 1 week ago by tpartner.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose