La validación de correo electrónico Javascript no funciona

What is wrong with my code? I can't find the solution.

var re = "/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/";

var email= document.getElementById("email").value;

if (re.test(email) == false) {  
    return false;

The double quotes caused the problem, thank you! I cant vote up because of my reputation :-(

preguntado el 24 de agosto de 12 a las 10:08

¿Por qué tan complicado? see here -

Tu dices us what's wrong. Do you get an error? Does it not validate correctly? Does it pass all input, or does it not pass any valid emails, or does it give false positives/negatives? -

Holy moley! That's a complex validation you have there! -

You don't need to vote up, just acepta la respuesta correcta. -

2 Respuestas

Like suresh suggested, you should remove the quotes (") around your regex string. So your first line should be:

var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

I produced a working example below:

function test()
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]    {1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  var email= "";

  if (re.test(email) == false) {  
    alert("invalid email address");
  alert("valid address");
<button onclick="test()">TEST</button>

Respondido 24 ago 12, 12:08

Lets try this once which is working smart for me

  function IsEmail(email) {
                var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                return regex.test(email);

                $("#somediv").text(" Email should be valid ");
                return false;

Respondido 24 ago 12, 10:08

People - the question is "What is wrong with my code". It's no "how to validate email addresses". - JJJ

@jim bamboo ..remove the "" to your regex string . - user1612220

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.