Bgl86
Bgl86

Reputation: 737

jQuery Validate - Email without top level domain valid?

i have the following input in my form:

<input class="input-group-field" id="email" name="email" type="email" size="1" required="">

I am using jQuery validation plugin to validate the form before an ajax request. It works fine, BUT why the hell is this email input valid:

abc@esd

So if I am not adding a top level domain it seems to be a valid email?

whereas a abc@esd. is invalid

Any Idea whats wrong or what I can do to prevent this?

The code:

 if ($("#valForm").valid()) {
   // Do Stuff
 }

I have not set up any rules

Thanks

Upvotes: 1

Views: 3045

Answers (4)

FR6
FR6

Reputation: 3167

Create a custom method:

$.validator.addMethod('emailtld', function(val, elem){
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if(!filter.test(val)) {
        return false;
    } else {
        return true;
    }
}, '*');

Then you can add the data attribute "data-rule-emailtld" to your input field:

<input class="input-group-field" id="email" name="email" type="email" size="1" required="" data-rule-emailtld="true">

Note: Answer from "Sharukh k shaji" use the proper regular expression but wasn't using jQuery Validation plugin.

Upvotes: 5

Ankur Bhadania
Ankur Bhadania

Reputation: 4148

Try this regular expression /^([\w-.]+@(gmail|yahoo|hotmail)(\.+)?[a-zA-Z])\/?/ and create validator methods

$(document).ready(function(){
  $.validator.methods.email = function( value, element ) {
     var email = /^([\w-.]+@(gmail|yahoo|hotmail)(\.+)?[a-zA-Z])\/?/;
    return email.test(value);
  }
  $('.validate').validate();

  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"></script>
<form class="validate">
		<input class="input-group-field" id="email" name="email" type="email" required="">
		<input class="input-group-field" id="submit" name="submit" type="submit"  >
	</form>

Upvotes: -1

Scorpio
Scorpio

Reputation: 2327

Apparantly, yes, a quick lookup on wikipedia yields interesting results as to what is allowed: Valid adresses as by wikipedia

Then again, should you allow everything the RFC spec allows? Probably not. Firstly, because some are pretty specific (local domain only, your example) and others are blatantly obscure like "()<>[]:,;@\\\"!#$%&'*+-/=?^_{}| ~.a"@example.org (extracted from wikipedia), secondly validating by the book (or in this case, the RFC) gives you a page-long regex nobody can read nor understand.

Since you're probably using an out-of-the-box feature it's up to you how to handle it. If you want to tweak it, go for a regex that is compliant with 99.8% of all adresses in the wild, and probably 100% of your customers. If not, just accept the inherent weirdness that is the email-adress.

Code-wise, other answers might provide more help on the "how to" matter, since I lack knowledge in that area.

Upvotes: 1

Sharukh k shaji
Sharukh k shaji

Reputation: 134

<input class="input-group-field" id="email" name="email" type="email" size="1" required="">

The above code validates whether the entered is email or not. That means it only looks for a '@' and some values before and after that.

To validate email you must do jquery validation by yourself.

var emailValidation = function ( email ) {
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

if ( !filter.test( email ) ) {
    return false;
} else {
    return true;
}}

The above code is a sample for email validation using regx and jquery.

Upvotes: 1

Related Questions