How can I do to validate an email in javascript?

0

I have this code that works correctly for "Ordinary" emails example: username + @ + server + domain [email protected]

/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.([a-zA-Z]{2,4})+$/

But what do I have to append to add the following?

username + @ + server + domain + country Example: [email protected]

Thank you very much.

    
asked by Aaron Alvarez 25.05.2018 в 00:15
source

1 answer

0

This is the pattern:

/^(([^<>()\[\]\.,;:\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,}))$/

$(function(){
  $(document).on('keyup','#foo',function(){
    var val = $(this).val().trim(),
        reg = /^(([^<>()\[\]\.,;:\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,}))$/;
    if( reg.test(val) == false ){
      console.log('NO es un mail');
    }
    
    else{
      console.log('SI es un mail');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="foo">

Lean on this tool Regexper is brutal.

    
answered by 25.05.2018 / 00:23
source