I'm fairly new to Limesurvey and Javascript. What I'd like to do is validate an email address and phone number(s) in a multiple short text question only if the field is filled in. I only need 2 of the 3 fields to be filled in. I found some Javascript in the workarounds section of the manual and here in this section of the forums. I tried to adapt it to my survey but it's just not working correctly. I'm running this on v2.05. I also dabbled with expression validation but I'm using a modified skeletonquest template that does show help text.
The current script runs and prevents using the Next button but it also doesn't allow me to move forward even with a valid email address. In the script I'm using I placed an alert and passed the emailInput variable in both the true and false sections of the if statement but it's returning nothing. I assume the script is just not reading/saving the input but I don't know how to fix it.
I also tried using a single variable in the function using the format answer189137X255X33321 but that doesn't return the error message from the script, just the default "you didn't answer all the questions" verbiage. Here is the code I'm using.
I haven't even began to work on validating the phone numbers because I haven't gotten the email to validate properly yet.
The current script runs and prevents using the Next button but it also doesn't allow me to move forward even with a valid email address. In the script I'm using I placed an alert and passed the emailInput variable in both the true and false sections of the if statement but it's returning nothing. I assume the script is just not reading/saving the input but I don't know how to fix it.
I also tried using a single variable in the function using the format answer189137X255X33321 but that doesn't return the error message from the script, just the default "you didn't answer all the questions" verbiage. Here is the code I'm using.
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Call the email function with the question ID and the row number
// 255 is the question ID and 1 is the row number which is the email field
emailTest(255, 1);
function emailTest(qID, inputNum) {
// Interrupt next/submit function
$('#movenextbtn, #movesubmitbtn').click(function(){
// Some vars - modify as required
var emailMatch = /^[a-zA-Z0-9\_\-]+[a-zA-Z0-9\.\_\-]*@([a-zA-Z0-9\_\-]+\.)+([a-zA-Z]{2,4}|travel|museum)$/;
var msg1 = 'Please enter a valid email address.';
// 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>
I haven't even began to work on validating the phone numbers because I haven't gotten the email to validate properly yet.