Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how can i validate a password without regex in js

function validate() {
  var p = document.getElementById('pass').value
  var errors = []

  //if (p.length < 8) {
  //  errors.push("Your password must be at least 8 characters")
  //}
  if (p.search(/[a-z]/) < 0) {
    errors.push("Your password must contain at least one lowercase letter.")
  }
  if (p.search(/[A-Z]/) < 0) {
    errors.push("Your password must contain at least one uppercase letter.")
  }
  if (p.search(/[0-9]/) < 0) {
    errors.push("Your password must contain at least one digit.")
  }
  if(p.search(/[!@#$\%^&*()\_+.,;:-]/) < 0) {
    errors.push("Your password must contain at least one special character.")
  }
    
  if (errors.length > 0) {
    document.getElementById("errors").innerHTML = errors.join("<br>")
    return false;
  }
  return true;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #validate #password #regex #js
ADD COMMENT
Topic
Name
2+4 =