Validate email specified as an open text of a single question

From Catglobe Wiki
Revision as of 13:55, 5 September 2011 by 10.1.5.13 (talk | contribs) (Script)
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 != ""){
               alert($(this).val());
               valid = validateEmail(email); 
           }        
        });
    });  
    if (!valid)
    {
       ErrorMessages.getInstance().showErrorMessage(ErrorMessage_InvalidEmail);
       return false;
    }
    else  
      return true;
}
questioncheck = extendedQuestionCheck;