problem using regular expression to validate e-mail

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 performs the validation looks like this:

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 tried the following "[email protected]", but it does not work, it returns me that the email is invalid, any suggestions?

    
asked by anonymous 27.02.2017 / 18:40

1 answer

2

I do not understand much of jquery to point out where the fault in your code occurs and it is incomplete, your problem is not the regular expression for the email you have tested.

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script></head><inputtype="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.

    
27.02.2017 / 19:54