0

I have a javascript code whch validates only integers of a password. All I want is to develop the code for "var exp", which the password must be a combination of uppercase characters, lowercase characters, numeric characters and a special character.

HTML codes are working perfectly..

//Empty Validation//
function isEmpty(elemValue, field) {
  if (elemValue == "" || elemValue == null) {
    alert(field + " must not be empty!");
    return true;
  } else {
    return false;
  }
}

//empty validation end//

//password validation//          
function pwValidation(elemValue) {
  if (!isEmpty(elemValue, "Password")) {
    if (elemValue.length >= 8) {
      var exp1 = /^[0-9]+$/;

      if (elemValue.match(exp1)) {
        return true;
      } else {
        alert("Password must contain Upper, lower case, number and at least a special character");
        return false;
      }
    } else {
      alert("Password must at least 8 characters!");
    }
  } else {
    return false;
  }
}
//password validation end//
Colin
  • 1,112
  • 1
  • 16
  • 27

1 Answers1

0

Take a look at this answer to previous question regarding password validation. As said, it's easier to change your regEx to find invalid passwords rather than valid, so you should flip your logic round, like this:

//Empty Validation//
function isEmpty(elemValue, field) {
  if (elemValue == "" || elemValue == null) {
    alert(field + " must not be empty!");
    return true;
  } else {
    return false;
  }
}

//empty validation end//


//password validation//          
function pwValidation(elemValue) {
  if (!isEmpty(elemValue, "Password")) {
    if (elemValue.length >= 8) {
      var exp1 = /^(.{0,7}|[^0-9]*|[^A-Z]*|[^a-z]*|[a-zA-Z0-9]*)$/;

      if (elemValue.match(exp1)) {
        alert("Password must contain Upper, lower case, number and at least a special character");
        return false;
      } else {
        return true;
      }
    } else {
      alert("Password must at least 8 characters!");
    }
  } else {
    return false;
  }
}
//password validation end//
Sam Conran
  • 156
  • 1
  • 7