Check out the LimeSurvey source code on GitHub!

insert javascript

More
4 years 9 months ago - 4 years 9 months ago #80542 by mattias01
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
Attachments:
Last Edit: 4 years 9 months ago by mattias01.

Please Log in to join the conversation.

More
4 years 9 months ago #80543 by TMSWhite
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 .

Please Log in to join the conversation.

More
4 years 9 months ago #80544 by mattias01
At the moment is a bit difficult to update to version 1.92, since I have lots of data.

regards

Please Log in to join the conversation.

More
4 years 9 months ago #80570 by tpartner
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.

Please Log in to join the conversation.

More
4 years 9 months ago - 4 years 9 months ago #80615 by DenisChenu
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 development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
Last Edit: 4 years 9 months ago by DenisChenu.

Please Log in to join the conversation.

More
4 years 9 months ago #80702 by mattias01
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

Please Log in to join the conversation.

More
4 years 9 months ago #80729 by Mazi
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)survey-consulting.com'"

Please Log in to join the conversation.

More
4 years 9 months ago #80736 by mattias01
this is the file I'm editing.
Attachments:

Please Log in to join the conversation.

More
4 years 8 months ago #80762 by tpartner
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 following user(s) said Thank You: mattias01

Please Log in to join the conversation.

More
4 years 8 months ago #80789 by mattias01
tparnert worked thank you very much for your excellent script.

regards

Please Log in to join the conversation.

More
4 years 8 months ago - 4 years 8 months ago #80793 by mattias01
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 8 months ago by mattias01.

Please Log in to join the conversation.

More
4 years 8 months ago - 4 years 8 months ago #80795 by tpartner
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 8 months ago by tpartner.

Please Log in to join the conversation.

More
4 years 8 months ago #80797 by mattias01
thanks works great.

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form