The problem appears to be a few things.

Email

1) You're selecting the email input with an id selector, so add the proper id.

    <input id='em' type='email' placeholder="Email" name='em'>  

This will allow your blur to actually get triggered.

Now, you will get an error in the console. myEmail is not defined, as it isn't. Go ahead and assign it to the value of the email input

    $('#em').blur(function () {
    myEmail = $('this').val();

2) Rather than applying invalid and valid to a div, you'll probably want to add them to a list item as you did in the username.

    <div id=email_info class=info>
       <h4> The email you typed is incorrect: </h4>
       <ul>
         <li id=eformat class=invalid> 
           A valid email format looks like: username@example.com
         </li>
       </ul>
    </div>

And then apply the fading in and out as you did before

      if (isValidEmail(myEmail)){ 
        $('#email_info').fadeOut('slow');
      } else {
        $('#email_info').fadeIn('slow');
      }

This will fix email. See your updated Gist here

Password Confirmation

As for password confirmation, you need to add the same id you're selecting again.

  <td><input id='pa2' type='password' placeholder='Repeat password' name=pa2></input>

Since you are selecting it with pa2 here

  $('#pa2').blur(function () {

Also, you'll need to define pswd along with the already defined pswd2.

  var pswd = $('#pa1').val();
  var pswd2 = $('#pa2').val();

This should fix both problems. See updated gist here

Answer from ruevaughn on Stack Overflow
🌐
Talkerscode
talkerscode.com › webtricks › validate-email-and-password-using-jquery.php
Validate Email And Password Using jQuery
July 1, 2023 - In this step we create a form to ... calls validate() function.In validate() function we get email and password value and write regular expressions for both email and password for validation....
🌐
YouTube
youtube.com › balaji admane
Email and password validation in JQuery - YouTube
hey , guys in this video im going to show how to validate email and password using JQuery , to get this code download it from github ,link :https://github.co...
Published   October 1, 2018
Views   20K
🌐
GeeksforGeeks
geeksforgeeks.org › jquery › form-validation-using-jquery
Form Validation using jQuery - GeeksforGeeks
July 15, 2025 - At the bottom of the <body> tag, include the "app.js" file having jQuery code for form validation. Create an app.js file that validates the whole form as given below in the code. In the app.js file, when the document is ready, hide all the error messages. Perform the validation task for all the input fields such as username, email, password, and confirm password.
🌐
YouTube
youtube.com › invention tricks
jQuery Password Validation | Password Strength Validate using jQuery - YouTube
Today in this video, you will learn how to validate password using HTML CSS and jQuery.▶️ More Free Tutorials 👇https://www.youtube.com/c/InventionTricks/vid...
Published   June 29, 2022
Views   6K
Top answer
1 of 2
2

The problem appears to be a few things.

Email

1) You're selecting the email input with an id selector, so add the proper id.

    <input id='em' type='email' placeholder="Email" name='em'>  

This will allow your blur to actually get triggered.

Now, you will get an error in the console. myEmail is not defined, as it isn't. Go ahead and assign it to the value of the email input

    $('#em').blur(function () {
    myEmail = $('this').val();

2) Rather than applying invalid and valid to a div, you'll probably want to add them to a list item as you did in the username.

    <div id=email_info class=info>
       <h4> The email you typed is incorrect: </h4>
       <ul>
         <li id=eformat class=invalid> 
           A valid email format looks like: username@example.com
         </li>
       </ul>
    </div>

And then apply the fading in and out as you did before

      if (isValidEmail(myEmail)){ 
        $('#email_info').fadeOut('slow');
      } else {
        $('#email_info').fadeIn('slow');
      }

This will fix email. See your updated Gist here

Password Confirmation

As for password confirmation, you need to add the same id you're selecting again.

  <td><input id='pa2' type='password' placeholder='Repeat password' name=pa2></input>

Since you are selecting it with pa2 here

  $('#pa2').blur(function () {

Also, you'll need to define pswd along with the already defined pswd2.

  var pswd = $('#pa1').val();
  var pswd2 = $('#pa2').val();

This should fix both problems. See updated gist here

2 of 2
1

Isn' t there a typo here?

    if( !isValidEmail(myEmail) ){
       $('#remail').removeClass('valid').addClass('invalid');
    }
    else{
       $('#remail').removeClass('valid').addClass('invalid');
    }

The both lines are equal!

🌐
CodePel
codepel.com › home › forms › email and password validation in javascript
Email and Password Validation in JavaScript — CodePel
January 27, 2024 - Here is a free Email and Password Validation in JavaScript , source code with preview. You can view demo online & download code.
Address   Rafi Qamar Road, Al-Majeed Peradise Al Majeed Peradise, 62300, Bahawalpur
🌐
W3Schools
w3schools.com › howto › howto_js_password_validation.asp
How To Create a Password Validation Form
Login Form Signup Form Checkout Form Contact Form Social Login Form Register Form Form with Icons Newsletter Stacked Form Responsive Form Popup Form Inline Form Clear Input Field Hide Number Arrows Copy Text to Clipboard Animated Search Search Button Fullscreen Search Input Field in Navbar Login Form in Navbar Custom Checkbox/Radio Custom Select Toggle Switch Check Checkbox Detect Caps Lock Trigger Button on Enter Password Validation Toggle Password Visibility Multiple Step Form Autocomplete Turn off autocomplete Turn off spellcheck File Upload Button Empty Input Validation · Filter List Filt
Find elsewhere
Top answer
1 of 2
2

Few issues we have in your current implementation:

a. The error you're likely getting is that password is undefined.

Right now you're doing:

var y=document.forms["myForm"]["password"].value;

but you refer to it as "password" further on:

var uppercase = password.match(/[A-Z]/)
var lowercase = password.match(/[a-z]/g)

change the var y to:

var password=document.forms["myForm"]["password"].value;

b. To validate email, you should use a Regex such as:

var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

var isValidEmail = re.test(email);

c. To check for the password rules, you should just rely on the regular expressions you have in place already (and strip out the atpos, dotpos usage - that makes it much more complicated than it even needs to be).

Example:

var email='[email protected]';
var password='test-P1assword';
var hasUpper = password.match(/[A-Z]/)
var hasLower = password.match(/[a-z]/g)
var hasNumber = password.match(/[0-9]/g)

var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

var isValidEmail = re.test(email);

if (isValidEmail && hasUpper && hasLower && hasNumber) {
    alert("Valid Email Address and Password");
    return true;
} else { 
    alert("Not a valid e-mail address or password");
    return false;
}

JSFiddle example, complete with Regex to validate email AND password: http://jsfiddle.net/4hH3T/2/

The regex was taken from: Validate email address in JavaScript?

2 of 2
0

you can use regex to validate.

 var reg=/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/;
 var isValid=reg.test(inputemail);
Top answer
1 of 2
5

You will need to divide the logic in your pwcheck into separate validator methods. Below is a prototype of the same:

var value = $("#password_reg").val();

$.validator.addMethod("checklower", function(value) {
  return /[a-z]/.test(value);
});
$.validator.addMethod("checkupper", function(value) {
  return /[A-Z]/.test(value);
});
$.validator.addMethod("checkdigit", function(value) {
  return /[0-9]/.test(value);
});
$.validator.addMethod("pwcheck", function(value) {
  return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) && /[a-z]/.test(value) && /\d/.test(value) && /[A-Z]/.test(value);
});

$('#signup-form').validate({
  rules: {
    password: {
      minlength: 6,
      maxlength: 30,
      required: true,
      //pwcheck: true,
      checklower: true,
      checkupper: true,
      checkdigit: true
    },
    confirmPassword: {
      equalTo: "#passwd_reg",
    },
  },
  messages: {
    password: {
      pwcheck: "Password is not strong enough",
      checklower: "Need atleast 1 lowercase alphabet",
      checkupper: "Need atleast 1 uppercase alphabet",
      checkdigit: "Need atleast 1 digit"
    }
  },
  highlight: function(element) {
    var id_attr = "#" + $(element).attr("id") + "1";
    $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
    $(id_attr).removeClass('glyphicon-ok').addClass('glyphicon-remove');
    $('.form-group').css('margin-bottom', '5px');
    $('.form').css('padding', '30px 40px');
    $('.tab-group').css('margin', '0 0 25px 0');
    $('.help-block').css('display', '');
  },
  unhighlight: function(element) {
    var id_attr = "#" + $(element).attr("id") + "1";
    $(element).closest('.form-group').removeClass('has-error').addClass('has-success');
    $(id_attr).removeClass('glyphicon-remove').addClass('glyphicon-ok');
    $('#confirmPassword').attr('disabled', false);
  },
  errorElement: 'span',
  errorClass: 'validate_cus',
  errorPlacement: function(error, element) {
    x = element.length;
    if (element.length) {
      error.insertAfter(element);
    } else {
      error.insertAfter(element);
    }
  }

});
.has-feedback .form-control-feedback {
  top: 33px;
}

.validate_cus {
  color: #a94442;
  font-size: small;
}

label {
  display: inline-block;
  margin-bottom: 5px;
  font-weight: 700;
}

.top-row > div {
  float: left;
  width: 48%;
  margin-right: 4%;
}

.field-wrap {
  position: relative;
  margin-bottom: 20px;
}

input,
textarea {
  font-size: 18px;
  display: block;
  height: 100%;
  width: 100%;
  padding: 5px 10px;
  background: none;
  background-image: none;
  border: 1px solid #a0b3b0;
  color: #545f58;
  border-radius: 6px;
  -webkit-transition: border-color .25s ease, box-shadow .25s ease;
  transition: border-color .25s ease, box-shadow .25s ease;
}
input:disabled {
    background: #eee;
}

.button:hover,
.button:focus {
  background: #0b9444;
}

.button-block {
  display: block;
  width: 50%;
}

.button {
  border: 0;
  outline: none;
  border-radius: 20px;
  padding: 15px 0;
  font-size: 1.6rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .1em;
  background: #187143;
  color: #ffffff;
  -webkit-transition: all 0.5s ease;
  transition: all 0.5s ease;
  -webkit-appearance: none;
}

#signup-form {
  padding: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/jquery.validate.min.js"></script>

<form id="signup-form" action="login" method="post">
  <div class="top-row">
    <div class="form-group has-feedback field-wrap">
      <label id="lbl_paswd" class="control-label" for="password">
        Password
        <span class="req">*
        </span>
      </label>
      <input type="password" name="password" id="password_reg" class="" required autocomplete="off" />
      <span class="glyphicon form-control-feedback" id="password_reg1">
      </span>
    </div>
    <div class="form-group has-feedback field-wrap">
      <label class="control-label" for="confirmPassword">
        Confirm Password
        <span class="req">*
        </span>
      </label>
      <input type="password" name="confirmPassword" id="confirmPassword" class="" disabled required autocomplete="off" />
      <span class="glyphicon form-control-feedback" id="confirmPassword1">
      </span>
    </div>
  </div>
  <button type="submit" class="button button-block">SIGN UP
  </button>
</form>

2 of 2
2

code:

    $.validator.addMethod("strongePassword", function(value) {
        return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) && /[a-z]/.test(value) && /\d/.test(value) && /[A-Z]/.test(value);
    },"The password must contain at least 1 number, at least 1 lower case letter, and at least 1 upper case letter"); 
        

Uses to :

  rules: { 
        password: {
                    required: !0,
                    strongePassword: true
                 },
     }

This worked for me.

🌐
HTML Form Guide
html.form.guide › jquery › validation-using-jquery-examples
Form Validation Using Jquery Examples | HTML Form Guide
For the email field, we add an email rule that will ensure the value entered is a valid email. The password field has an added rule that ensures its minimum length is 8 characters. After specifying the rules, we set the error messages that will be shown whenever a rule fails.
Top answer
1 of 4
17

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.

2 of 4
4

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);
}
🌐
The Art of Web
the-art-of-web.com › javascript › validate-password
Password Validation using regular expressions and HTML5
Using JavaScript to confirm username and password input format. Examples of form validation using both simple and complex regular expressions. Restricting to alphanumeric and letter characters.
🌐
DevExtreme
js.devexpress.com › jQuery › Demos › WidgetsGallery › Demo › Validation › Overview
JavaScript/jQuery Validation - Overview | jQuery/JS Example
$(() => { const maxDate = new Date(); maxDate.setFullYear(maxDate.getFullYear() - 21); const sendRequest = function (value) { const invalidEmail = 'test@dx-email.com'; const d = $.Deferred(); setTimeout(() => { d.resolve(value !== invalidEmail); }, 1000); return d.promise(); }; const changePasswordMode = function (name) { const editor = $(name).dxTextBox('instance'); editor.option('mode', editor.option('mode') === 'text' ? 'password' : 'text'); }; $('#summary').dxValidationSummary({ }); $('#email-validation').dxTextBox({ inputAttr: { 'aria-label': 'Email' }, }) .dxValidator({ validationRules:
🌐
Peachpit
peachpit.com › articles › article.aspx
Adding JavaScript Validation | A Complete(-ish) Login System Using jQuery | Peachpit
In the latter case, error messages ... will just confirm that each input is not empty. That code looks like this: ... First, jQuery grabs a reference to the email input using $('#email')....
🌐
Tech Solution Stuff
techsolutionstuff.com › post › how-to-validate-password-and-confirm-password-using-jquery
How To Validate Password And Confirm Password Using JQuery
March 29, 2024 - In this tutorial I will show you how to validate password and confirm password using jquery. Validation is basic and important feature for authenticate user. So, here I will give you example about password and confirm password validation using jquery.
🌐
TutorialsPoint
tutorialspoint.com › How-to-validate-email-using-jQuery
How to validate email using jQuery?
June 20, 2020 - <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function() { $('.error').hide(); $('#submit').click(function(){ var name = $('#name').val(); var email = $('#email').val(); if(name== ''){ $('#name').next().show(); return false; } if(email== ''){ $('#email').next().show(); return false; } if(IsEmail(email)==false){ $('#invalid_email').show(); return false; } $.post("", $("#myform").serialize(), function(response) { $('#myform').fadeOut('slow',function(){ $('#correct').html(response); $('#correct').f