7

My script javascript is like this :

email: {
         required: true,
         email: true          
},

When in the textfield email, I write : chelsea@gmail without .com, it's valid.

Any solution to solve my problem?

Thank you

Sparky
  • 98,165
  • 25
  • 199
  • 285
moses toh
  • 12,344
  • 71
  • 243
  • 443
  • as per jquery-validation plugin this should work. Are you getting any error in console? – vijayP Jun 03 '16 at 05:07
  • i had this problem you just change like this name filed email_field: { required: true, email: true }, – JYoThI Jun 03 '16 at 05:13
  • `admin@localhost` is an example of email address without `.com .org .net` – keziah Jun 03 '16 at 05:29
  • As per the [version 1.12 changelog](https://github.com/jzaefferer/jquery-validation/blob/master/changelog.md#1120--2014-04-01), the jQuery Validate plugin is now following the HTML5 regex for a valid email address, which no longer requires a TLD. – Sparky Jun 03 '16 at 12:54

4 Answers4

14

Edited Answer:

To validate whether an email address is of the name@domain.tld format you can use the following regular expression:

var emailExp = new RegExp(/^\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i);

This regular expression accepts only email addresses that contain an @-sign, a dot and a 2-4 characters long TLD.

You can use the above regular expression to validate a given email address as shown below:

function validate_email (email) {
   /* Define the recommended regular expression. */
   var emailExp = new RegExp(/^\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i);

   /* Test the email given against the expression and return the result. */
   return emailExp.test(email);
}

jQuery Validator:

jQuery Validator doesn't support the use of regular expressions and instead uses the default HTML5 email regular expression internally, so you must first create a new method for the validator that does that:

$.validator.addMethod(
    /* The value you can use inside the email object in the validator. */
    "regex",

    /* The function that tests a given string against a given regEx. */
    function(value, element, regexp)  {
        /* Check if the value is truthy (avoid null.constructor) & if it's not a RegEx. (Edited: regex --> regexp)*/

        if (regexp && regexp.constructor != RegExp) {
           /* Create a new regular expression using the regex argument. */
           regexp = new RegExp(regexp);
        }

        /* Check whether the argument is global and, if so set its last index to 0. */
        else if (regexp.global) regexp.lastIndex = 0;

        /* Return whether the element is optional or the result of the validation. */
        return this.optional(element) || regexp.test(value);
    }
);

Now that a method supporting validation against a regular expression was created for the validator, you can use the jQuery.validate as follows:

$('#element_id').validate({
    email: {
        required: true,
        email: true,
        regex: /^\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i
    }
});

Original Answer (Improved):

To filter an email address and only accept a format like name@domain.tld use this regular expression:

var emailExp = new RegExp(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i);

This regular expression filters out any "junk" that may be entered and requires that an @-sign, a dot and a 2-4 characters long TLD be present. If a substring of the given email address matches it the substring is returned, otherwise false.

You can use the above regular expression to filter a given email address as shown below:

function filter_email (email) {
   var
      /* Define the recommended regular expression. */
      emailExp = new RegExp(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i),

      /* Use 'match' to filter the email address given and cache the result. */
      filtered = email.match(emailExp);

   /* Return the filtered value or false. */
   return filtered ? filtered[0] : false;
}

Notes:

  • When answering the OP's question more than a year ago, I mistook his intention for email validation as an attempt to filter a given string keeping only a substring of it that is an email address.

  • This answer considers addresses lacking a TLD invalid even though they are perfectly valid in the real world as per OP's request.

SirSaleh
  • 1,452
  • 3
  • 23
  • 39
Angel Politis
  • 10,955
  • 14
  • 48
  • 66
  • Where the script was added? I use jquery validate – moses toh Jun 03 '16 at 05:07
  • @mosestoh I edited the answer. – Angel Politis Jun 03 '16 at 05:11
  • 1
    This answer would also validates `test@test.com,test2@test.com` as true, although its not valid. – Adam Nov 27 '17 at 21:03
  • You are absolutely right @Adam! When I wrote this answer a year plus ago, I mistook the OP's request for `email filtering` rather than `email validation`. Apparently, my answer was serving a different purpose, so I edited to correct that. I hope you find it useful. _Thanks a lot for your comment_. – Angel Politis Nov 27 '17 at 23:27
3

try this

function isEmail(email) {
  var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}
Abdul Hameed
  • 263
  • 4
  • 19
3
$(function() {
// Setup form validation on the #register-form element
$("#login-form").validate({
    // Specify the validation rules
    rules: {
        email: {
            required: true,
            email: true
        }
    },
    // Specify the validation error messages
    messages: {         
        email: "Please enter a valid email address"
    },
    submitHandler: function(form) {
        form.submit();
    }
});   });

i hope it will be helpful

https://jsfiddle.net/dave17/bex78vvs/

Dave
  • 3,073
  • 7
  • 20
  • 33
1
var re=new RegExp();
    re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

if(re.test(Email_Value)){
   alert("valid email");
}
CRSSJ
  • 252
  • 5
  • 18