Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Multi short text validation

  • jaystone
  • jaystone's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 1 month ago #148546 by jaystone
Multi short text validation was created by jaystone
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.
Code:
<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 != '' &amp;&amp; !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.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 1 month ago #148554 by tpartner
Replied by tpartner on topic Multi short text validation
I think the problem is that you cannot use curly braces in regular expressions place in the question source - Expression Manager will cause them to be parsed inaccurately. I would place the function in template.js and simply call it from the question source.

So, this in template.js:
Code:
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 != '' &amp;&amp; !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;
    }
  });
}

And this in the question source:
Code:
<script type="text/javascript" charset="utf-8">$(document).ready(function() {
 
    // Call the email function with the question ID and the row number
    // 1 is the row number which is the email field
    emailTest({QID}, 1);
  }); 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • jaystone
  • jaystone's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 1 month ago #148624 by jaystone
Replied by jaystone on topic Multi short text validation
Tried moving the code around like you suggested but it didn't seem to have an effect. I tinkered with the code and got mixed results. I added some output statements to try to figure out what is and isn't working. When the email text box is empty is works, but no matter what format the email is in, it returns invalid. Here is the current script. I also tested removing the brackets from the regex where it checks the length and that didn't help either.
Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).ready(function() {
 
  // Call the email function with the text input
  emailTest("answer189137X255X33321");
 
    function emailTest(inputID) {
 
      // 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,6})$/;
        var msg1 = 'Please enter a valid email address.';
 
        // Test the input
        var emailInput = $("#"+inputID).val();
        if(emailInput == "") {
          alert("No email present");
          return false;
        }
        else if(emailInput != "" &amp;&amp; !emailMatch.test(emailInput)) {
          alert(emailInput+' I');
          $('#question'+qID+' li:eq('+(inputNum-1)+') input.text').css({ 'background':'pink' });
          return false;
        }
        else {
          alert(emailInput+' V');
          $('#question'+qID+' li:eq('+(inputNum-1)+') input.text').css({ 'background':'' });
          return true;
        }
      });
    }
  });
 
</script>
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose