Difference between revisions of "Validate email specified as an open text of a single question"

From Catglobe Wiki
Jump to: navigation, search
m (Solution)
(Script)
Line 13: Line 13:
 
==Script==
 
==Script==
 
<source lang=javascript>
 
<source lang=javascript>
var qLabel = "Q1";//question's label
+
var qLabel = "Question1";//question's label
 
var openAnswerValue = 1;//the value of answer option which has open text
 
var openAnswerValue = 1;//the value of answer option which has open text
 
var ErrorMessage_InvalidEmail = "Invalid email.";//error message in your needed language
 
var ErrorMessage_InvalidEmail = "Invalid email.";//error message in your needed language
 
    
 
    
var oldQuestionCheck = this.questioncheck;
+
var normalQuestionCheck = questioncheck;
questioncheck = function()
+
function extendedQuestionCheck()
 
{
 
{
  if (!oldQuestionCheck())
+
    var valid = normalQuestionCheck();
  return false;
+
    var email = $("input[name='QUESTION." + qLabel + ".Open." + openAnswerValue +"']").val();
 
+
    $(document).ready(function(){
  var email = $("input[name='QUESTION." + qLabel + ".Open." + openAnswerValue +"']").val();
+
        $("input:radio[name='QUESTION." + qLabel+"']").each(function(index) {
 
+
 
  if (!validateEmail(email))
+
          if($(this).val() == openAnswerValue && email != ""){
  {
+
              alert($(this).val());
      ErrorMessages.getInstance().showErrorMessage(ErrorMessage_InvalidEmail);
+
              valid = validateEmail(email);
      return false;
+
          }       
  }
+
        });
 
+
    }); 
  return true;
+
    if (!valid)
 +
    {
 +
      ErrorMessages.getInstance().showErrorMessage(ErrorMessage_InvalidEmail);
 +
      return false;
 +
    }
 +
    else 
 +
      return true;
 
}
 
}
 +
questioncheck = extendedQuestionCheck;
 
</source>
 
</source>

Revision as of 12:55, 5 September 2011

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;