Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate email using jquery validate? [duplicate]

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

like image 888
moses toh Avatar asked Jun 03 '16 05:06

moses toh


People also ask

How to validate duplicate email in javascript?

keyup(function() { //Get input fields values var $email1 = $("input[name='email']"). val(); var $email2 = $("input[name='confirmEmail']"). val(); //Check if email are identical. if($email1 === $email2) { console.

How to check duplicate email in php using Ajax?

php'; $name=$_POST['name']; $email=$_POST['email']; $phone=$_POST['phone']; $city=$_POST['city']; $duplicate=mysqli_query($conn,"select * from crud where email='$email'"); if (mysqli_num_rows($duplicate)>0) { echo json_encode(array("statusCode"=>201)); } else{ $sql = "INSERT INTO `crud`( `name`, `email`, `phone`, `city ...

What is validate in jQuery?

Form validation is a process of confirming the relevant information entered by the user in the input field. Here we will be validating a simple form that consists of a username, password and a confirmed password using jQuery.


4 Answers

Edited Answer:

To validate whether an email address is of the [email protected] 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 [email protected] 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.

like image 160
Angel Politis Avatar answered Nov 17 '22 21:11

Angel Politis


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);
}
like image 31
Abdul Hameed Avatar answered Nov 17 '22 20:11

Abdul Hameed


$(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/

like image 42
Dave Avatar answered Nov 17 '22 22:11

Dave


var re=new RegExp();
    re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

if(re.test(Email_Value)){
   alert("valid email");
}
like image 38
CRSSJ Avatar answered Nov 17 '22 21:11

CRSSJ