Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email not validating completely with jQuery validation

Email address not validating completely for example. i have a email field and i am validating it from jQuery validation plugin. when i put some like "abbcss" it says email is not valid. then i put "abbcss@g" the error is gone and as you can see the email is still not valid. for better understanding i put a fiddle here.

$(document).ready(function(e) {
    $('#email').keyup(function(){
			$('#checkform').validate();
	 });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="//getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="//cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.js"></script>


<form id="checkform">
<input id="email" type="email" name="email">
</form>
<hr>
<p>Try to put first "abcc" and click outside field </p>
<p>then try to put "abcc@gmail" and click outside field </p>
<p>it will consider it a valid email but actually it is not.</p>

any help regarding this issue will be appriciate.

like image 266
JunaidFarooqui Avatar asked Apr 09 '16 08:04

JunaidFarooqui


People also ask

How check email ID is valid or not in jQuery?

Approach: You can validate Email using jQuery by using the regex pattern. Regex expression is used for search operation and they are special strings representing a pattern to be matched.

How do I validate an email address with regex?

With that in mind, to generally validate an email address in JavaScript via Regular Expressions, we translate the rough sketch into a RegExp : let regex = new RegExp('[a-z0-9]+@[a-z]+\.

How do I validate an email address without regex?

Javascript(JS) Email Validation without Regex Email Address string must contain "@" sign. "."(dot) must be last character in string to test. consecutive "." (dot) should not be there. Length of string must be at-least 2 characters long.


1 Answers

You can validate email by using jquery validation plugin add method

jQuery.validator.addMethod("validate_email", function(value, element) {

    if (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(value)) {
        return true;
    } else {
        return false;
    }
}, "Please enter a valid Email.");

$('#checkform').validate({
    rules: {
        email: {
            validate_email: true
        },
    }
});
like image 198
Saravanan N Avatar answered Sep 28 '22 18:09

Saravanan N