problem using regular expression to validate e-mail

Asked

Viewed 352 times

0

Hi, I’m using the following regular expression to validate an email field:

  var filtro = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;

the function that validates:

if(email.val() === ""){
   $("#valida_email").show();//mostra o <p> com a mensagem de validação
   passou=false;//quando retornar verdadeiro dá o submito no form em outra função
 } else if(filtro.test(email) === false){
   $("#valida_email").html("Preencha com um E-mail válido");
   $("#valida_email").show();
   passou = false;
 } else if (filtro.test(email) ===true){
   $("#valida_email").hide(); 
 }

i tested as follows "[email protected]", but it doesn’t work, it returns to me that the email is invalid, any suggestion?

1 answer

2


I don’t know much about jquery to point out where your code fails and it’s incomplete, your problem is not the regular expression for the email you tested.

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<input type="text" id="email"/>
<script>
$(document).ready(function()
{	$('#email').focusout(function()
	{	$('#email').filter(function()
		{	var emil=$('#email').val();
            var emailReg = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
            if(!emailReg.test(emil))
            {	alert('Correto');
            }else
            {	alert('Incorreto');
            }
        })
    });
});
</script>

So I made this example so you can adapt it.

  • Okay! Reverti, good you said so I can fix :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.