Difference between revisions of "Force email address"

From Catglobe Wiki
Jump to: navigation, search
(Challenge)
(Code)
Line 8: Line 8:
 
== Code ==
 
== Code ==
 
<source lang="javascript" line="1">
 
<source lang="javascript" line="1">
//function to validate if an email address
 
function isEmail(str)
 
{
 
var emailFormat = /^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-zA-Z]{2,6}(\.[a-zA-Z]{2})?$/i;
 
var emailAdress = str;
 
if(emailAdress.search(emailFormat)==-1)
 
return false;
 
else
 
return true;
 
}
 
 
 
var normalQuestionCheck = questioncheck;
 
var normalQuestionCheck = questioncheck;
 
function extendedQuestionCheck()
 
function extendedQuestionCheck()
Line 30: Line 19:
 
  val = document["query"][quest.label + "." + 2].value;
 
  val = document["query"][quest.label + "." + 2].value;
 
            if (val.length > 0)
 
            if (val.length > 0)
valid = isEmail(val);               
+
valid = validateEmail(val);               
 
}
 
}
 
if (!valid)  
 
if (!valid)  

Revision as of 03:38, 3 March 2009

Challenge

You want the respondent to write an e-mail address.The script needs to validate if a string input is in email address format

Example


ValidateEmail.JPG

Code

 1 var normalQuestionCheck = questioncheck;
 2 function extendedQuestionCheck()
 3 {
 4 	 var valid = normalQuestionCheck();
 5   	 if (valid) 
 6 	 {
 7       	        var val;
 8 		//The value below is the index value of the sub question where your e-mail question is asked.
 9 		//In this case we use the sub question has index 2 
10  		val = document["query"][quest.label + "." + 2].value;
11 	            	if (val.length > 0)
12 			valid = validateEmail(val);               
13 	 }
14 	 if (!valid) 
15 	 { 
16  	        alert('Wrong email address format');
17                 return false;
18          }
19 	 return true;
20 }