2016-04-12 18 views
-1

Ich habe unten Code verwendet und es funktioniert gut, bitte überprüfen Sie unten.Email Validierung in jQuery

$(".emailValidation").change(function(){ 

    $('body .emailError').remove(); 
    var emailVal = this.value; 
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
    if(filter.test(emailVal) === false) 
    { 
     $(".emailValidation").after("<div class='emailError'>You have added wrong email address.</div>"); 
    } 
    else 
    { 
     $('.emailError').remove(); 
    } 

}); 

Aber wenn Benutzer passieren [email protected] dann diese Überprüfung nicht funktioniert.

+2

[email protected] ist eine gültige E-Mail-Adresse. Lehnt die Validierung sie ab und Sie möchten, dass sie besteht, oder akzeptiert sie sie und Sie möchten, dass sie abgelehnt wird? – JJJ

+0

@Juhana die Validierung Regex würde [email protected], die - wie Sie sagten - eine gültige Adresse ist. – Lochlan

+0

@Juhana Ich brauche nur E-Mail-Adresse [email protected] und wenn Benutzer eine andere Zeichenfolge übergeben, dann wird es abgelehnt. –

Antwort

-1

Probieren Sie diesen regulären Ausdruck, es funktioniert jedes Mal.

/^ [- a-z0-9 ~ $%^& _ = +} {\ '?] + ([-.! A-z0-9 ~ $%^& _ = +} {\ '?] +) @ ([a-z0-9 _] [- a-z0-9_] (. [- a-z0-9 _] +) *. (Aero | arpa | biz | com | coop | edu | gov | info | int | mil | museum | name | netz | org | pro | reise | mobi | [az] [az]) | ([0-9] {1,3}. [0- 9] {1,3}. [0-9] {1,3}. [0-9] {1,3})) (: [0-9] {1,5})? $/I

Probieren Sie das Code-Schnipsel !!!

$('form input[name="email"]').keyup(function() { 
 
    var email = $(this).val(); 
 
var re = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i; 
 
if (re.test(email)) { 
 
    $('.msg').hide(); 
 
    $('.success').show(); 
 
} else { 
 
    $('.msg').hide(); 
 
    $('.error').show(); 
 
} 
 

 
});
.msg { 
 
    display: none; 
 
} 
 
.error { 
 
    color: red; 
 
} 
 
.success { 
 
    color: green; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<form method="post"> 
 
    <div> 
 
     <label for="email" id="email-ariaLabel">Your email address:</label> 
 
     <input id="email" name="email" type="email" class="required" title="This is a required field" /> <span class="msg error">Not a valid email address</span> 
 
     <span class="msg success">A valid email address!</span> 
 

 
    </div> 
 
</form>

-2

Bitte versuchen Sie es mit folgenden Code. Dies läuft für:

 

    [email protected]@gmail.com 
    [email protected]@gmail.com.com 
    [email protected] 

 

     $(".emailValidation").change(function(){ 

      $('body .emailError').remove(); 
      var emailVal = this.value; 
      var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
      if (emailVal.indexOf("@") >= 0){ 
       var emailExt = emailVal.split('@'); 
       var extCount = (emailExt[1].match(/.com/g) || []).length; 
      }   

      if(filter.test(emailVal) === false || extCount>1) 
      { 
       $(".emailValidation").after( "You have added wrong email address."); 
       console.log("wrong email id") 
      } 
      else if(extCount === 1) 
      { 
       $('.emailError').remove(); 
       console.log("Email is correct"); 
      } 
     }); 


+0

Dieser Code ist funktioniert nicht. –

+0

@JoomlaDeveloper: Bitte versuchen Sie den obigen Code und stimmen Sie ab, wenn es für Sie funktioniert :) –