Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

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

Validation for only one of the answers in multiple short text question 2 years 11 months ago #60299

  • Samba
  • Samba's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 29
  • Karma: 0
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
The administrator has disabled public write access.

Re: Validation for only one of the answers in multiple short text question 2 years 11 months ago #60334

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Validation for only one of the answers in multiple short text question 2 years 11 months ago #60341

  • Samba
  • Samba's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 29
  • Karma: 0
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:
The administrator has disabled public write access.

Re: Validation for only one of the answers in multiple short text question 2 years 11 months ago #60343

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
...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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Validation for only one of the answers in multiple short text question 2 years 5 months ago #67876

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 170
  • Thank you received: 1
  • Karma: 3
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: 2 years 5 months ago by waitz.
The administrator has disabled public write access.

Re: Validation for only one of the answers in multiple short text question 2 years 5 months ago #67884

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Validation for only one of the answers in multiple short text question 2 years 5 months ago #67887

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 170
  • Thank you received: 1
  • Karma: 3
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 |
The administrator has disabled public write access.

Re: Validation for only one of the answers in multiple short text question 2 years 5 months ago #67889

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Validation for only one of the answers in multiple short text question 2 years 5 months ago #67891

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5279
  • Thank you received: 289
  • Karma: 246
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)limesurvey.org'"
The administrator has disabled public write access.

Re: Validation for only one of the answers in multiple short text question 2 years 5 months ago #68107

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 170
  • Thank you received: 1
  • Karma: 3
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 |
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.199 seconds
Donation Image