Adding validation to text field - multiple short text

More
5 years 10 months ago #71524 by sameera
sameera replied the topic: Adding validation to text field - multiple short text
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.

Please Log in to join the conversation.

More
5 years 10 months ago #71525 by tpartner
tpartner replied the topic: Adding validation to text field - multiple short text
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.

Please Log in to join the conversation.

More
5 years 10 months ago #71526 by sameera
sameera replied the topic: Adding validation to text field - multiple short text
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>

Please Log in to join the conversation.

More
5 years 10 months ago #71528 by tpartner
tpartner replied the topic: Adding validation to text field - multiple short text
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.
The following user(s) said Thank You: sameera

Please Log in to join the conversation.

More
5 years 10 months ago #71529 by TMSWhite
TMSWhite replied the topic: Adding validation to text field - multiple short text

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 following user(s) said Thank You: sameera

Please Log in to join the conversation.

More
5 years 10 months ago #71531 by sameera
sameera replied the topic: Adding validation to text field - multiple short text
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>

Please Log in to join the conversation.

More
5 years 10 months ago #71532 by sameera
sameera replied the topic: Adding validation to text field - multiple short text
Thank you TOM for the information. I am using Version 1.91+ and will need to upgrade to the current version.

Please Log in to join the conversation.

More
5 years 10 months ago #71561 by Mazi
Mazi replied the topic: Adding validation to text field - multiple short text
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)survey-consulting.com'"

Please Log in to join the conversation.

More
5 years 10 months ago #71694 by miradoro
miradoro replied the topic: Adding validation to text field - multiple short text
this is the code i have now
38194x3x115
but i have a multiple short choice (SQ012)


So i used
38194x3x115xSQ012 but it didnt work :(

Please Log in to join the conversation.

More
5 years 10 months ago #71697 by miradoro
miradoro replied the topic: Adding validation to text field - multiple short text
I also used _

var email = $("#answer38194X3X115_SQ012");

but it does not work

Please Log in to join the conversation.

More
5 years 10 months ago #71709 by tpartner
tpartner replied the topic: Adding validation to text field - multiple short text
If the question is a multiple-short-text type, the selector should be "#answer38194X3X115SQ012".

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 10 months ago #71710 by miradoro
miradoro replied the topic: Adding validation to text field - multiple short text
thanks let me try that

Please Log in to join the conversation.

More
5 years 10 months ago #71716 by Mazi
Mazi replied the topic: Adding validation to text field - multiple short text
You can set Limesurvey to show all codes at the printable survey screen, see this setting:
"$showsgqacode: (>1.91) This setting is used at the printable survey feature Image and defaults to false. If you set $showsgqacode = true; the IDs for each question - and answer if applicable - will be shown; these IDs match the column heading at the Lime_survey_12345 table which holds the answer data for a certain survey. These Ids can be used for a code book for manual database queries."
Link to optional settings


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 10 months ago #71720 by tpartner
tpartner replied the topic: Adding validation to text field - multiple short text
Or use Firebug for Firefox to inspect the element.

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 10 months ago #71728 by miradoro
miradoro replied the topic: Adding validation to text field - multiple short text
the validation works
but the problem is that i am using groups

the email is on the first group...when u go to next group i still see the validation..i am using js script

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!