Difference between revisions of "Force email address"
(→Code) |
(→Code) |
||
Line 23: | Line 23: | ||
var valid = normalQuestionCheck(); | var valid = normalQuestionCheck(); | ||
if (valid) | if (valid) | ||
− | + | { | |
− | + | var val; | |
− | |||
//The value below is the index value of the sub question where your e-mail question is asked. | //The value below is the index value of the sub question where your e-mail question is asked. | ||
//In this case we use the sub question has index 2 | //In this case we use the sub question has index 2 | ||
Line 31: | Line 30: | ||
if (val.length > 0) | if (val.length > 0) | ||
valid = isEmail(val); | valid = isEmail(val); | ||
− | + | } | |
if (!valid) | if (!valid) | ||
{ | { |
Revision as of 10:39, 2 March 2009
Challenge
You want the respondent to write an e-mail address.
Example
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 }