function validEmail (emailAddress)
{
for (var index=0; index < emailAddress.length; index++)
{
if ( emailAddress[index] == "@")
{
for (var count=0; count <emailAddress.length; count++)
{
if (emailAddress[count] == ".")
{
return true;
}
}
}
else
{
return false;
}
}
}
function btnParseAddress_onclick()
{
var emailAddress = addressTextbox.value;
var userName = emailUsername(emailAddress);
var domain = emailDomain (emailAddress);
var valid = validEmail (emailAddress);
if (valid)
{
outputTextbox.value = "Username:" + userName + "\nDomain:" + domain
}
else
{
outputTextbox.value = "Invalid Email Address"
}
}
Now the point of this assignment is to return a username and a domain from an inputted email address. I have deleted other functions and variables to help focus better on the problem.
I need to validate the email address first. I need to make sure there is an “@” and a “.” within the string entered and return the value with true or false. True having the strings and false not having the strings. When I run the file, the return value is always false. I can’t figure out if it is my for loop in the validEmail function or the if statement in btnParseAddress_onclick function
You can avoid the
forloop by usingIndexOftwice: