Programming Tutorials

Custom Password Object in JavaScript

By: aathishankaran in JavaScript Tutorials on 2007-03-27  

In JavaScript, there is no built-in Password object. However, you can create a custom object to handle passwords. Here's an example:

let Password = {
  minLength: 8,
  maxLength: 20,
  validate: function (password) {
    if (password.length < this.minLength) {
      return "Password is too short";
    } else if (password.length > this.maxLength) {
      return "Password is too long";
    } else {
      return "Password is valid";
    }
  }
};

// Example usage
console.log(Password.validate("password")); // Password is too short
console.log(Password.validate("supersecret")); // Password is valid
console.log(Password.validate("averylongpasswordthatexceedsthemaxlength")); // Password is too long

In this example, the Password object has properties for the minimum and maximum length of the password, as well as a validate method that checks if a password is valid based on these criteria. The validate method uses the this keyword to refer to the Password object's minLength and maxLength properties.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in JavaScript )

Latest Articles (in JavaScript)