Can someone give me a help, is not validating the email.

-2
$('#username').focusout(function () {
    //atribuindo valor do campo
    var sEmail = $("username").val();
    // filtros
    var emailFilter = /^.+@.+\..{2,}$/;
    var illegalChars = /[\(\)\<\>\,\;\:\\/\"\[\]]/; 
    // condição
    if (!(emailFilter.test(sEmail)) || sEmail.match(illegalChars)) {
        $("#icon_ok").hide();
        $("#icon_cancel").show();

    } else {
        $("#icon_cancel").hide();
        $("#icon_ok").show();
    }  
});

*** NOTE: If you do not type anything, it has no effect.

    
asked by anonymous 25.05.2018 / 15:46

1 answer

0

Missed a game of the old !! instead of $("username").val() must be $("#username").val()

To not display icons when not typing anything, make a new if and check that the field is not null!

$('#username').focusout(function () {
    //atribuindo valor do campo
    var sEmail = $("#username").val();
    // filtros
    var emailFilter = /^.+@.+\..{2,}$/;
    var illegalChars = /[\(\)\<\>\,\;\:\\/\"\[\]]/; 
    // condição
    //verifica se campo não é nulo
    if (sEmail!=""){
      if (!(emailFilter.test(sEmail)) || sEmail.match(illegalChars)) {
        $("#icon_ok").hide();
        $("#icon_cancel").show();

      } else {
        $("#icon_cancel").hide();
        $("#icon_ok").show();
      }
    }  
});
#icon_ok,#icon_cancel {
display: none;

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputid="username">

<img id="icon_ok" src="https://i.stack.imgur.com/82F83.png"><imgid="icon_cancel" src="https://i.stack.imgur.com/1cR0t.png">
    
25.05.2018 / 16:56