Force email address

From Catglobe Wiki
Revision as of 10:37, 2 March 2009 by Catglobe (talk | contribs) (Code)
Jump to: navigation, search

Challenge

You want the respondent to write an e-mail address.

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         		// val = email i form
19 	                	var val;
20 		//The value below is the index value of the sub question where your e-mail question is asked.
21 		//In this case we use the sub question has index 2 
22  		val = document["query"][quest.label + "." + 2].value;
23 	            	if (val.length > 0)
24 			valid = isEmail(val);               
25 	}
26 	 if (!valid) 
27 	 { 
28  	        alert('Wrong email address format');
29                 return false;
30          }
31 	 return true;
32 }