0

I'm trying to check an email field on my website but the function that I have always returns false. The regular expression seems right but the function is not working properly.

Here is the code:

function CheckEmail(email)
{
//regular expression to check the email field
var expr = /^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/;

if (expr.test(email)) {
    document.getElementById('alertEmail').innerHTML = '<img title="Valid Email" src="images/icoOk.jpg" alt="Ok"/>';
    f = true;
    return true;
}
else {
    document.getElementById('alertEmail').innerHTML = '<img title="Invalid Email!" src="images/icoErro.gif" alt="Erro"/>';
    f = false;
    return false;
}
}

any ideias??

Thank y'all so much for the help

duhzecca
  • 115
  • 1
  • 2
  • 8

1 Answers1

0

Your function works fine if you pass a proper string to it, See JSFiddle to play with it. try to output the email variable to the console or alert it to see the value:

console.log(email);

or

alert(email);

as the first line of your function.

Empty strings will always return false from your method.

NullPointer
  • 442
  • 3
  • 7
  • In my page it always shows that the email is invalid :/ – duhzecca Jul 21 '14 at 21:29
  • I tried to put the alert and it shows [objectHTMLInputElement] – duhzecca Jul 21 '14 at 21:31
  • By the way this is the input that I'm trying to get the email. It is an image that works as a textbox. When it gets the focus it just replace the image for another image without any text so the user can type the email – duhzecca Jul 21 '14 at 21:50
  • You need .value on the element to get the actual string value of the input. In your onblur try this: CheckEmail(document.getElementById('enterEmail').value) – NullPointer Jul 21 '14 at 23:47