Hey;
I am learning some javascript. I tried to write a trivial code to validate email but my code does not seem to work it should.
It should turn the background color of text box green when mail contains both '@' and '.' characters and red otherwise. However what the code does is turning it to green whenever there is a '@' not caring about the '.'.
What is wrong with the code ?
function CheckMail()
{
var box = document.getElementById("email");
var mail = box.value;
var valid = true;
if((mail.search("@") == -1) || (mail.search(".") == -1)) // If mail contains "@" and "." it is valid
valid = false;
if(valid)
{
box.style.background = "green";
}
else
{
box.style.background = "red";
}
}