Difference between revisions of "Force email address"

From Catglobe Wiki
Jump to: navigation, search
(Code)
(Challenge)
Line 1: Line 1:
 
== Challenge ==  
 
== Challenge ==  
You want the respondent to write an e-mail address.  
+
You want the respondent to write an e-mail address.The script need to validate if a string input is in email address format
 +
 
 
== Example ==  
 
== Example ==  
 
<br>
 
<br>

Revision as of 10:40, 2 March 2009

Challenge

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

Example


ValidateEmail.JPG

Code

 1 //function to validate if an email address 
 2 function isEmail(str)
 3 {
 4 	var emailFormat = /^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-zA-Z]{2,6}(\.[a-zA-Z]{2})?$/i;
 5 	var emailAdress = str;
 6 	if(emailAdress.search(emailFormat)==-1)
 7 		return false;
 8 	else 
 9 		return true;
10 }
11 
12 var normalQuestionCheck = questioncheck;
13 function extendedQuestionCheck()
14 {
15 	 var valid = normalQuestionCheck();
16   	 if (valid) 
17 	 {
18       	        var val;
19 		//The value below is the index value of the sub question where your e-mail question is asked.
20 		//In this case we use the sub question has index 2 
21  		val = document["query"][quest.label + "." + 2].value;
22 	            	if (val.length > 0)
23 			valid = isEmail(val);               
24 	 }
25 	 if (!valid) 
26 	 { 
27  	        alert('Wrong email address format');
28                 return false;
29          }
30 	 return true;
31 }