Someone can give me a help,’s not validating the email.

Asked

Viewed 48 times

-3

$('#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, do not have any effect.

  • 3

    $("username").val() shouldn’t be $("#username").val() or $(this).val()?

  • Our brother, what a detail!!! Thank you very much !!!

  • @Andersoncarloswoss how do I make the icons disappear if I do not type anything, pq qnd I click on the field and do not type anything, qnd I click off, the icon appears, in the case as I did not type it was not to give any effect

  • How do I make it already validate as the user type? Why does it only validate when I click out...

1 answer

0


We missed an old game!! instead of $("username").val() must be $("#username").val()

To not display icons when typing nothing, 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>


<input id="username">

<img id="icon_ok" src="https://i.stack.imgur.com/82F83.png">

<img id="icon_cancel" src="https://i.stack.imgur.com/1cR0t.png">

  • Guys, how do I make it already validating as the user type? Why is it only valid when I click out...

Browser other questions tagged

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