Check out the LimeSurvey source code on GitHub!
Welcome, Guest
Username: Password:

TOPIC: insert javascript

insert javascript 4 years 3 months ago #80542

  • mattias
  • mattias's Avatar
  • Offline
  • Senior Lime
  • Posts: 79
  • Karma: 1
I have a function where I try to evaluate two numerical fields where the value 1 must be <= value 2

example



my script
<script type="text/javascript" charset="utf-8">
function validation()
{
   if (({INSERTANS:27832X11242X124482}<={INSERTANS:27832X11242X124483}))
   {
 
     document.limesurvey.move.value = 'moveprev';
     document.limesurvey.submit();
   }
   else
   {
	 alert("value 1 must be <= to value 2");
     document.limesurvey.move.value = 'movelast';
     document.limesurvey.submit();
   }
}
 
setTimeout("validation()",250);
 
</script>

regards
Last Edit: 4 years 3 months ago by mattias.
The administrator has disabled public write access.

insert javascript 4 years 3 months ago #80543

  • TMSWhite
  • TMSWhite's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 758
  • Thank you received: 83
  • Karma: 37
If you use 1.92+, you don't need the JavaScript. Instead, use the "minimum value" advanced question option.

Screen-shots and working examples can be found here.
The administrator has disabled public write access.

insert javascript 4 years 3 months ago #80544

  • mattias
  • mattias's Avatar
  • Offline
  • Senior Lime
  • Posts: 79
  • Karma: 1
At the moment is a bit difficult to update to version 1.92, since I have lots of data.

regards
The administrator has disabled public write access.

insert javascript 4 years 3 months ago #80570

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6199
  • Thank you received: 1374
  • Karma: 538
Are both questions on the same page?
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

insert javascript 4 years 3 months ago #80615

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • Moderator Lime
  • Posts: 9295
  • Thank you received: 1330
  • Karma: 386
Hello,

No need a time out function.
Try:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("input.submit").click(function(){
   if ($("#question124482 input.text").val()<=$("#question124483 input.text").val()))
   {
       return true
   }
   else
   {
	 alert("value 1 must be <= to value 2");
     return false;
   }
});
});
</script>
( maube some error in code, but basis is here).
replace the number by the question real number.
Denis
Assistance on LimeSurvey forum and LimeSurvey core developpement are on my free time (Say thanks ?).
A bug not reported is a bug not corrected. | Please, read the documentation | La doc en français à besoin de vous
Last Edit: 4 years 3 months ago by DenisChenu.
The administrator has disabled public write access.

insert javascript 4 years 3 months ago #80702

  • mattias
  • mattias's Avatar
  • Offline
  • Senior Lime
  • Posts: 79
  • Karma: 1
Denis and tpartner thanks, but I throw an error code, the questions are on the same page, also change the codes of the questions.

regards
The administrator has disabled public write access.

insert javascript 4 years 3 months ago #80729

  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5977
  • Thank you received: 368
  • Karma: 260
Please activate a test survey and post the link here so we can have a look at the HTML source.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

insert javascript 4 years 3 months ago #80736

  • mattias
  • mattias's Avatar
  • Offline
  • Senior Lime
  • Posts: 79
  • Karma: 1
this is the file I'm editing.
Attachments:
The administrator has disabled public write access.

insert javascript 4 years 3 months ago #80762

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6199
  • Thank you received: 1374
  • Karma: 538
I would validate the inputs dynamically instead of waiting for the submit click.

Assuming there are only two numeric questions on the page, you can use this without messing with IDs.

The script puts listeners on the inputs and if a second value is found to be higher than the first, the respondent is alerted and the last value entered is removed.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		var text = 'The first value must not be higher than the second. Please re-enter your value.';
 
		$('.numeric input.text').change(function(){
			if($('.numeric:eq(0) input.text').val() && $('.numeric:eq(1) input.text').val() && $('.numeric:eq(0) input.text').val() > $('.numeric:eq(1) input.text').val()) {
				alert (text);
				$(this).val('');
			}
		});
	});
 
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.
The following user(s) said Thank You: mattias

insert javascript 4 years 3 months ago #80789

  • mattias
  • mattias's Avatar
  • Offline
  • Senior Lime
  • Posts: 79
  • Karma: 1
tparnert worked thank you very much for your excellent script.

regards
The administrator has disabled public write access.

insert javascript 4 years 3 months ago #80793

  • mattias
  • mattias's Avatar
  • Offline
  • Senior Lime
  • Posts: 79
  • Karma: 1
And if there are more than two questions as I add the identifier.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		var text = 'El número clientes en el pasillo debe ser mayor o igual al número de clientes siendo atendidos.';
		var pregunta1 = "#question124482";
		var pregunta2 = "#question124483";
 
		$('.numeric input.text').change(function(){
			if($('.numeric:eq(pregunta1) input.text').val() && $('.numeric:eq(pregunta2) input.text').val() && $('.numeric:eq(pregunta1) input.text').val() > $('.numeric:eq(pregunta2) input.text').val()) {
				alert (text);
				$(this).val('');
			}
		});
	});
 
</script>
Last Edit: 4 years 3 months ago by mattias.
The administrator has disabled public write access.

insert javascript 4 years 3 months ago #80795

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6199
  • Thank you received: 1374
  • Karma: 538
You are close:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		var text = 'El número clientes en el pasillo debe ser mayor o igual al número de clientes siendo atendidos.';
		var pregunta1 = "#question124482";
		var pregunta2 = "#question124483";
 
		$(''+pregunta1+' input.text, '+pregunta2+' input.text'').change(function(){
			if($(''+pregunta1+' input.text').val() && $(''+pregunta2+' input.text').val() && $(''+pregunta1+' input.text').val() > $(''+pregunta2+' input.text').val()) {
				alert (text);
				$(this).val('');
			}
		});
	});
 
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 4 years 3 months ago by tpartner.
The administrator has disabled public write access.

insert javascript 4 years 3 months ago #80797

  • mattias
  • mattias's Avatar
  • Offline
  • Senior Lime
  • Posts: 79
  • Karma: 1
thanks works great.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.479 seconds
Imprint                   Data Protection Statement                  Revocation information and revocation form