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

JavaScript must be enabled for certain features to work
* Required information
1000

Comments

No comments yet. Be the first!
(追記) (追記ここまで)
(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /