Welcome, Guest
Username: Password: Remember me

TOPIC: Adding validation to text field - multiple short text

Re: Adding validation to text field - multiple short text 2 years 9 months ago #71524

  • sameera
  • sameera's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 4
  • Karma: 0
Hello,

I am new to Limesurvey and thank you for this awesome software.:)

I was trying to use above java script to validate 'time' in a multiple array question.

However when I insert this script to array question, the respective input field become a mandatory field. User is forced to insert an input to the field. But I want to keep it as a non-mandatory field.

So is there a way to execute validation function only if the user enter any text input into the field?

Can anyone help to resolve this issue. Thank you in advance.
The administrator has disabled public write access.

Re: Adding validation to text field - multiple short text 2 years 9 months ago #71525

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4270
  • Thank you received: 804
  • Karma: 368
Try this:
<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 || email == '') {
				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 2 years 9 months ago #71526

  • sameera
  • sameera's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 4
  • Karma: 0
Thank you Tony for your quick reply.

I tried you script. But still I am forced to enter email address in the text field.I can not navigate to next page without answering to this question.

This is another java script that I found in this forum. It is also used for the same validation function multiple short text questions. It is executed only if I enter some input into the text field. But unfortunately I can not find a way to adapt this script for multiple array questions.

your thoughts? Thank you.

Here is the said script

<script type="text/javascript" charset="utf-8">

$(document).ready(function() {

// Call the email function with the question ID and the row number
emailTest(24,1);

function emailTest(qID, inputNum) {

// Interrupt next/submit function
$('#movenextbtn, #movesubmitbtn').click(function(){

// Some vars - modify as required
var emailMatch = /^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$/;
var msg1 = 'Please enter time in correct format.';

// Test the input
var emailInput = $('#question'+qID+' li:eq('+(inputNum-1)+') input.text').val();
if(emailInput != '' && !emailMatch.test(emailInput)) {
alert(msg1);
$('#question'+qID+' li:eq('+(inputNum-1)+') input.text').css({'background':'pink'});
return false;
}
else {
$('#question'+qID+' li:eq('+(inputNum-1)+') input.text').css({'background':''});
return true;
}
});
}
});

</script>
The administrator has disabled public write access.

Re: Adding validation to text field - multiple short text 2 years 9 months ago #71528

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4270
  • Thank you received: 804
  • Karma: 368
Assuming that the ID of the input field is "answer31822X121X22232":
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Call the email function with the input ID
		emailTest('answer31822X121X22232');
 
		function emailTest(inputID) {
 
			// Interrupt next/submit function
			$('#movenextbtn, #movesubmitbtn').click(function(){
 
				// Some vars - modify as required
				var emailMatch = /^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$/;
				var msg1 = 'Please enter time in correct format.';
 
				// Test the input
				var emailInput = $('#'+inputID+'').val();
				if(emailInput != '' && !emailMatch.test(emailInput)) {
					alert(msg1);
					$('#question'+qID+' li:eq('+(inputNum-1)+') input.text').css({'background':'pink'});
					return false;
				}
				else {
					$('#question'+qID+' li:eq('+(inputNum-1)+') input.text').css({'background':''});
					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.
The following user(s) said Thank You: sameera

Re: Adding validation to text field - multiple short text 2 years 9 months ago #71529

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
zahidansari wrote:
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.

This features is available in LimeSurvey 1.92 RC1. 1.92 RC2 adds support for validation within Array Multi-Flexi Text. In 1.92 RC2, there is an onchange() even that turns the background color of the input field red if it contains an invalid value.

/Tom
The administrator has disabled public write access.
The following user(s) said Thank You: sameera

Re: Adding validation to text field - multiple short text 2 years 9 months ago #71531

  • sameera
  • sameera's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 4
  • Karma: 0
Hi Tony, Thank you very much, you helped me to resolve this. Your second code is working fine, though I had to make a tiny adjustment to work it perfectly.

After inserting your above code I got the validation error message but when click 'Ok' on error message, I had no chance to correct the error. Next page is loaded. Also I wanted to highlight the error field.

I have no any JavaScript knowledge, but doing few trial and error, I was able make the needed adjustments to your code :)

Again thanking Tony for your incredibly fast response and solution.

Here is the final code, may be useful for someone like me :)


<script type="text/javascript" charset="utf-8">

$(document).ready(function() {

// Call the email function with the input ID
emailTest('answer74827X3X113SQ01_SQ001');

function emailTest(inputID) {

// Interrupt next/submit function
$('#movenextbtn, #movesubmitbtn').click(function(){

// Some vars - modify as required
var emailMatch = /^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$/;
var msg1 = 'Please enter time in correct format.';


// Test the input
var emailInput = $('#'+inputID+'').val();
if(emailInput != '' && !emailMatch.test(emailInput)) {
alert(msg1);
$('#'+inputID+'').css({'background':'pink'});

return false;

}
else {
$('#question'+qID+' li:eq('+(inputNum-1)+') input.text').css({'background':''});
return true;
}
});
}
});

</script>
The administrator has disabled public write access.

Re: Adding validation to text field - multiple short text 2 years 9 months ago #71532

  • sameera
  • sameera's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 4
  • Karma: 0
Thank you TOM for the information. I am using Version 1.91+ and will need to upgrade to the current version.
The administrator has disabled public write access.

Re: Adding validation to text field - multiple short text 2 years 9 months ago #71561

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5331
  • Thank you received: 296
  • Karma: 249
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 2 years 9 months ago #71694

  • miradoro
  • miradoro's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Karma: 0
this is the code i have now
38194x3x115
but i have a multiple short choice (SQ012)


So i used
38194x3x115xSQ012 but it didnt work :(
The administrator has disabled public write access.

Re: Adding validation to text field - multiple short text 2 years 9 months ago #71697

  • miradoro
  • miradoro's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Karma: 0
I also used _

var email = $("#answer38194X3X115_SQ012");

but it does not work
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.136 seconds
Donation Image