Validate email specified as an open text of a single question

From Catglobe Wiki
Jump to: navigation, search

Challenge

In order to secure my questionnaire's input
As a CGS writer
I want to validate an email specified as an open text of a single question

Example

Validate email - semi open question.png

Solution

  • Add the script below to the question's Javascript property
  • Locate the input text box and use validateEmail function to validate the email

Script

var qLabel = "Question1";//question's label
var openAnswerValue = 1;//the value of answer option which has open text
var ErrorMessage_InvalidEmail = "Invalid email.";//error message in your needed language
   
var normalQuestionCheck = questioncheck;
function extendedQuestionCheck()
{
    var valid = normalQuestionCheck();
    var email = $("input[name='QUESTION." + qLabel + ".Open." + openAnswerValue +"']").val();
     $(document).ready(function(){ 
        $("input:radio[name='QUESTION." + qLabel+"']").each(function(index) {

           if($(this).val() == openAnswerValue && email != ""){
               valid = validateEmail(email); 
           }        
        });
    });  
    if (!valid)
    {
       ErrorMessages.getInstance().showErrorMessage(ErrorMessage_InvalidEmail);
       return false;
    }
    else  
      return true;
}
questioncheck = extendedQuestionCheck;

Source

Questionnaire Resource Id on cg.catglobe.com site: 164079 (Question: Q15_Validate_email_specified_as_an_open_text)