Check out the LimeSurvey source code on GitHub!

Validation for only one of the answers in multiple short text question

More
5 years 9 months ago #60299 by Samba
Hi,

I have an address question where i need to capture the information in three parts,
Street, Landmark and finally Zipcode.

I have created a multiple short text question with three fields as mentioned above.

But In our country, the Zipcode is always only 6 digits no more no less.

What I want is that only in that field, the user should be able to enter only six digits.

For this, in the question.pstpl of the templete i have inserted the following code
<script type="text/javascript">$(document).ready(function(){$("#answer66911X1X5SQ003").attr('maxlength','6');$("#answer66911X1X5SQ003").css('width','50px');$("#answer66911X1X5SQ003").keypress(function (e){if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){return false;}});});</script>

This lets the user enter only maximum 6 characters which are only numbers.

So far so good, but the user is able to also proceed if there are less than 6 digits.

I have tried a lot to no success. Please HELP!!:(


Regards,
Samba

Please Log in to join the conversation.

More
5 years 9 months ago #60334 by tpartner
Interrupt the next/submit function, find the zip string length, alert and abort the submit if it doesn't equal 6:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Interrupt next/submit function 
		$('form#limesurvey').submit(function(){
 
			var msg = 'The zip code must be 6 characters.';
			var zip = $("#answer66911X1X5SQ003").val();
 
			if(zip.length != 6) {
				alert (msg);
				return false;
			}
			else {
				return true;
			}
		});
	});
 
</script>

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 9 months ago #60341 by Samba
Hi,

Thanks for the code, even my friend did somethin similar for me, but in both your and his case, the alert keeps on coming (14 times to be precise).

Also the previous button gets disbled when the code is used

Please note that the survey is set to be group by group and the group for this question has 14 questions, Version 1.90+ Build 9642

I am really stuck here.. Please help! :ohmy:

Please Log in to join the conversation.

More
5 years 9 months ago #60343 by tpartner

...the alert keeps on coming (14 times to be precise).

Did you paste the code in all questions?

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 4 months ago - 5 years 4 months ago #67876 by waitz

tpartner wrote: Interrupt the next/submit function, find the zip string length, alert and abort the submit if it doesn't equal 6:

<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Interrupt next/submit function 
		$('form#limesurvey').submit(function(){
 
			var msg = 'The zip code must be 6 characters.';
			var zip = $("#answer66911X1X5SQ003").val();
 
			if(zip.length != 6) {
				alert (msg);
				return false;
			}
			else {
				return true;
			}
		});
	});
 
</script>

I need to check 2 things:
1. To set the zip length max 6 characters, what is the expression I use instead of zip.length != 6?

2. When I have several subquestions with different max lengths, do I just add one script after the other in the main question (with it's correct var zip = $("#answer66911X1X5SQ003").val();) ?

Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
Last Edit: 5 years 4 months ago by waitz.

Please Log in to join the conversation.

More
5 years 4 months ago #67884 by tpartner

1. To set the zip length max 6 characters, what is the expression I use instead of zip.length != 6?

zip.length > 6

2. When I have several subquestions with different max lengths, do I just add one script after the other in the main question...

I would place the code in a function and then call it with specific input IDs, max lengths and messages. Something like:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		maxChars($("#answer66911X1X5SQ003"), 6, 'The zip code must be maximum 6 characters.');
		maxChars($("#answer66911X1X5SQ004"), 10, 'The phone number must be maximum 10 characters.');
 
		function maxChars(inputID, maxNum, msg) {
 
			if($(inputID).length > 0) {
 
				// Interrupt next/submit function 
				$('#movenextbtn, #movesubmitbtn').click(function(){
 
					if($(inputID).val().length > maxNum) {
						$(inputID).css({'color':'red'});
						alert (msg);
						return false;
					}
					else {
						$(inputID).css({'color':''});
						return true;
					}
				});
			}
		}
	});
</script>

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 4 months ago #67887 by waitz
Very nice. Thanks Tony. And just to be sure. I have another script that does something else in the same question. Is it ok to put one script after another?

Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |

Please Log in to join the conversation.

More
5 years 4 months ago #67889 by tpartner
It should be all right but I can't say for sure without seeing the other script.

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 4 months ago #67891 by Mazi

waitz wrote: Very nice. Thanks Tony. And just to be sure. I have another script that does something else in the same question. Is it ok to put one script after another?

That should be no problem.


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 3 months ago #68107 by waitz
Hmmm. This script works fine in a multiple short text, but it doesn't in a multiple choice with comments. Is there are reason for it?
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		maxChars($("#answer68164X55X913ref01"), 80, 'The answer can be maximum 80 characters.');
		maxChars($("#answer68164X55X913ref02"), 80, 'The answer can be maximum 80 characters.');
 
		function maxChars(inputID, maxNum, msg) {
 
			if($(inputID).length > 0) {
 
				// Interrupt next/submit function 
				$('#movenextbtn, #movesubmitbtn').click(function(){
 
					if($(inputID).val().length > maxNum) {
						$(inputID).addClass('maxCharsError');
						alert (msg);
						return false;
					}
					else {
						$(inputID).removeClass('maxCharsError');
						return true;
					}
				});
			}
		}
	});
</script>

Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |

Please Log in to join the conversation.

More
5 years 3 months ago #68113 by tpartner
I don't think you are using the correct input IDs. I think you need to append "comment" to them.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		maxChars($("#answer68164X55X913ref01comment"), 80, 'The answer can only be a maximum of 80 characters.');
		maxChars($("#answer68164X55X913ref02comment"), 80, 'The answer can only be a maximum of 80 characters.');
 
		function maxChars(inputID, maxNum, msg) {
 
			if($(inputID).length > 0) {
 
				// Interrupt next/submit function 
				$('#movenextbtn, #movesubmitbtn').click(function(){
 
					if($(inputID).val().length > maxNum) {
						$(inputID).addClass('maxCharsError');
						alert (msg);
						return false;
					}
					else {
						$(inputID).removeClass('maxCharsError');
						return true;
					}
				});
			}
		}
	});
</script>

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 3 months ago - 5 years 3 months ago #68125 by waitz
Yepp, of course. And I could have seen the inputID with firebug, I realize. I will look there first next time.

Thanks again, though :)

Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
Last Edit: 5 years 3 months ago by waitz.

Please Log in to join the conversation.

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