Difference between revisions of "Force email address"
(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...) |
|||
(7 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | == | + | == Force email address == |
− | You want the respondent to write an e-mail address. | + | |
− | + | You want the respondent to write an e-mail address.The script needs to validate if a string input is in email address format | |
− | + | ||
− | [[Image:ValidateEmail.JPG]] | + | In order to check email address. |
− | + | ||
− | == | + | As a questionnaire creator |
− | + | ||
− | + | I want to validate if a string input is in email address format when the respondent writes an e-mail address. | |
− | + | ||
− | + | '''Example''' | |
− | + | ||
− | + | [[Image:ValidateEmail.JPG]] | |
− | + | ||
− | + | === Solution === | |
− | + | *Add a Text grid question to Questionnaire editor like below | |
− | + | *Go to menu Properties -> Question scripts -> Java script tab -> Input script | |
− | + | ||
+ | [[Image:ForceEmail Code.jpg]] | ||
+ | |||
+ | === Code === | ||
+ | <source lang="javascript"> | ||
var normalQuestionCheck = questioncheck; | var normalQuestionCheck = questioncheck; | ||
function extendedQuestionCheck() | function extendedQuestionCheck() | ||
Line 23: | Line 27: | ||
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 | ||
val = document["query"][quest.label + "." + 2].value; | val = document["query"][quest.label + "." + 2].value; | ||
if (val.length > 0) | if (val.length > 0) | ||
− | valid = | + | valid = validateEmail(val); |
− | + | } | |
if (!valid) | if (!valid) | ||
− | + | { | |
− | + | alert('Wrong email address format'); | |
− | + | return false; | |
− | + | } | |
return true; | return true; | ||
} | } | ||
− | </source> | + | questioncheck = extendedQuestionCheck; |
+ | </source> | ||
+ | |||
+ | === Source === | ||
+ | |||
+ | Questionnaire Resource Id on cg site: 159730 |
Latest revision as of 10:39, 12 January 2012
Force email address
You want the respondent to write an e-mail address.The script needs to validate if a string input is in email address format
In order to check email address.
As a questionnaire creator
I want to validate if a string input is in email address format when the respondent writes an e-mail address.
Example
Solution
- Add a Text grid question to Questionnaire editor like below
- Go to menu Properties -> Question scripts -> Java script tab -> Input script
Code
var normalQuestionCheck = questioncheck;
function extendedQuestionCheck()
{
var valid = normalQuestionCheck();
if (valid)
{
var val;
//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
val = document["query"][quest.label + "." + 2].value;
if (val.length > 0)
valid = validateEmail(val);
}
if (!valid)
{
alert('Wrong email address format');
return false;
}
return true;
}
questioncheck = extendedQuestionCheck;
Source
Questionnaire Resource Id on cg site: 159730