More actions
Line 1: | Line 1: | ||
== Challenge == | == Challenge == | ||
You want the respondent to write an e-mail address.The script | You want the respondent to write an e-mail address.The script needs to validate if a string input is in email address format | ||
== Example == | == Example == |
Revision as of 09:40, 2 March 2009
Challenge
You want the respondent to write an e-mail address.The script needs to validate if a string input is in email address format
Example
Code
//function to validate if an email address
function isEmail(str)
{
var emailFormat = /^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-zA-Z]{2,6}(\.[a-zA-Z]{2})?$/i;
var emailAdress = str;
if(emailAdress.search(emailFormat)==-1)
return false;
else
return true;
}
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 = isEmail(val);
}
if (!valid)
{
alert('Wrong email address format');
return false;
}
return true;
}