Welcome, Guest
Username: Password: Remember me

TOPIC: Adding validation to text field - multiple short text

Adding validation to text field - multiple short text 3 years 1 month ago #66528

Hi All,

I have a question which asks the individual for their email and phone number(question type is multiple short text). But as I can see that there is no validation for particular option box. Can anyone suggest how can I accomplish that.

I have tried everything from past 2 week with no success. I am able to prompt user with alert box that email is not valid but then after clicking ok it moves to next page.


Any suggestions will be highly appreciated.
Thanks
The administrator has disabled public write access.

Re: Adding validation to text field - multiple short text 3 years 4 weeks ago #66547

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4293
  • Thank you received: 810
  • Karma: 373
You will need to use JavaScript to interrupt the Next/Submit function (form submission) and validate the inputs. If the validation fails you can abort the Next/Submit function.

Something like this. Replace "11111" with the survey ID, "22" with the group ID, "33" with the question ID and "44" with the sub-question ID.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function () {
 
		// Interrupt the submit function
		$('form#limesurvey').submit(function () {
			// Override the built-in "disable navigation buttons" feature
			$('#moveprevbtn, #movenextbtn, #movesubmitbtn').attr('disabled', '');
 
			if ($('#answer11111X22X3344').val() != 'some validation') {
				alert ('The email is invalid!');
				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: Adding validation to text field - multiple short text 3 years 4 weeks ago #66553

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5348
  • Thank you received: 298
  • Karma: 250
I recommend to split this up into two short text fields and then use a regular expression for data validation: docs.limesurvey.org/Using+regular+expres...tions+for+LimeSurvey

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: Adding validation to text field - multiple short text 3 years 3 weeks ago #66599

@tpartner,

Well I tried your code and I have jumped over the major road block and it is interupting the submit function but dont know why it is not validating the email field.:angry:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function () {
 
	var emailExp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        var email    = $("#answer31822X121X22232");
        var flag     = emailExp.test(email.val());
 
		// Interrupt the submit function
 
		$('form#limesurvey').submit(function () {
			// Override the built-in "disable navigation buttons" feature
			$('#moveprevbtn, #movenextbtn, #movesubmitbtn').attr('disabled', '');
                        //alert("FLAG ="+flag);
			if (flag) {
				return true;
			} else {
                                alert ('The email is invalid!');
				return false;
			}
		});
	});
</script>


Well the flag value is always coming as false...:blink:
Last Edit: 3 years 3 weeks ago by zahidansari.
The administrator has disabled public write access.

Re: Adding validation to text field - multiple short text 3 years 3 weeks ago #66616

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5348
  • Thank you received: 298
  • Karma: 250
I'm no JS expert but maybe you have to quote the expression so that /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/
becomes
"/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/"

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: Adding validation to text field - multiple short text 3 years 3 weeks ago #66632

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4293
  • Thank you received: 810
  • Karma: 373
You need to move the "email" and "flag" vars into the submit function so they are not created when the page loads.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function () {
 
		var emailExp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
 
		// Interrupt the submit function
		$('form#limesurvey').submit(function () {
			// Override the built-in "disable navigation buttons" feature
			$('#moveprevbtn, #movenextbtn, #movesubmitbtn').attr('disabled', '');
 
			var email = $("#answer31822X121X22232");
			var flag = emailExp.test(email.val());
 
			if (flag) {
				return true;
			} else {
				alert ('The email is invalid!');
				return false;
			}
		});
	});
</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: Adding validation to text field - multiple short text 3 years 3 weeks ago #66639

Thanks Tony you have saved the day..

its resolved
The administrator has disabled public write access.

Re: Adding validation to text field - multiple short text 3 years 3 weeks ago #66653

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5348
  • Thank you received: 298
  • Karma: 250
zahidansari wrote:
Thanks Tony you have saved the day..

its resolved
You're welcome!

If our hints have been helpful and you enjoy limesurvey please consider a donation to the team .
We do all this in our free time and you don't have to pay a penny for this software.

Without your help we can't keep this project alive.

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: Adding validation to text field - multiple short text 3 years 5 days ago #67835

  • miradoro
  • miradoro's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Karma: 0
Where do u place this code?
The administrator has disabled public write access.

Re: Adding validation to text field - multiple short text 3 years 5 days ago #67836

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4293
  • Thank you received: 810
  • Karma: 373
In the source of any question on the page. See - docs.limesurvey.org/tiki-index.php?page=..._etc._in_LimeSurvey_
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.
Moderators: ITEd
Time to create page: 0.130 seconds
Donation Image