insert javascript

More
5 years 1 week ago - 5 years 1 week ago #80542 by mattias01
mattias01 created the topic: insert javascript
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: 5 years 1 week ago by mattias01.

Please Log in to join the conversation.

More
5 years 1 week ago #80543 by TMSWhite
TMSWhite replied the topic: insert javascript
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
5 years 1 week ago #80544 by mattias01
mattias01 replied the topic: insert javascript
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
5 years 1 week ago #80570 by tpartner
tpartner replied the topic: insert javascript
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
5 years 1 week ago - 5 years 1 week ago #80615 by DenisChenu
DenisChenu replied the topic: insert javascript
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).
An error happen ? Before make a new topic : remind the Debug mode .
Last Edit: 5 years 1 week ago by DenisChenu.

Please Log in to join the conversation.

More
5 years 6 days ago #80702 by mattias01
mattias01 replied the topic: insert javascript
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
5 years 6 days ago #80729 by Mazi
Mazi replied the topic: insert javascript
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
5 years 6 days ago #80736 by mattias01
mattias01 replied the topic: insert javascript
this is the file I'm editing.
Attachments:

Please Log in to join the conversation.

More
5 years 5 days ago #80762 by tpartner
tpartner replied the topic: insert javascript
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
5 years 5 days ago #80789 by mattias01
mattias01 replied the topic: insert javascript
tparnert worked thank you very much for your excellent script.

regards

Please Log in to join the conversation.

More
5 years 5 days ago - 5 years 5 days ago #80793 by mattias01
mattias01 replied the topic: insert javascript
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: 5 years 5 days ago by mattias01.

Please Log in to join the conversation.

More
5 years 5 days ago - 5 years 5 days ago #80795 by tpartner
tpartner replied the topic: insert javascript
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: 5 years 5 days ago by tpartner.

Please Log in to join the conversation.

More
5 years 5 days ago #80797 by mattias01
mattias01 replied the topic: insert javascript
thanks works great.

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now