Difference between revisions of "Force email address"

From Catglobe Wiki
Jump to: navigation, search
(New page: == Challenge == You want the respondent to write an e-mail address. == Example == <br> Image:ValidateEmail.JPG <br> == Code == <source lang="javascript" line="1"> //function to vali...)
 
(Code)
Line 33: Line 33:
 
}
 
}
 
if (!valid)  
 
if (!valid)  
{  
+
{  
                alert('Wrong email address format');
+
        alert('Wrong email address format');
                                  return false;
+
                return false;
                  }
+
        }
 
return true;
 
return true;
 
}
 
}
 
</source>
 
</source>

Revision as of 11:37, 2 March 2009

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 }