screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html lang="en"> <head> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="app"> <h3>Vue Js Username and Password Validation</h3> <form> <label> Username: <input type="text" v-model="username" /> </label> <label> Password: <input type="password" v-model="password" /> <div v-if="password !== ''"> Password strength: {{ passwordStrength }} </div> </label> <button @click.prevent="submitForm">Submit</button> <p v-if="showValidationMessage">{{ validationMessage }}</p> </form> </div> <script type="module"> const app = new Vue({ el: "#app", data() { return { username: "", password: "", showValidationMessage: false, minUsernameLength: 3, maxUsernameLength: 20, minPasswordLength: 8, maxPasswordLength: 30, }; }, methods: { submitForm() { if (this.isValid()) { // Perform form submission logic } else { this.showValidationMessage = true; } }, isValid() { if ( this.username.length < this.minUsernameLength || this.username.length > this.maxUsernameLength || this.password.length < this.minPasswordLength || this.password.length > this.maxPasswordLength ) { return false; } // Use regular expressions to check the format of the username and password // Return false if the format is invalid if (!/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/.test(this.password)) { this.validationMessage = "Password must contain at least 1 letter and 1 number"; return false; } return true; }, checkPasswordStrength() { // Use a password strength library or regular expressions to determine the strength of the password // Update the passwordStrength computed property with a string that describes the strength of the password let passwordStrength = "Weak"; if (/(?=.*[A-Z])(?=.*[a-z])(?=.*\d).{8,}/.test(this.password)) { passwordStrength = "Strong"; } else if (/(?=.*[a-zA-Z])(?=.*\d).{6,}/.test(this.password)) { passwordStrength = "Medium"; } return passwordStrength; }, }, computed: { validationMessage() { let message = ""; if (this.username.length < this.minUsernameLength) { message += `Username must be at least ${this.minUsernameLength} characters long. `; } if (this.username.length > this.maxUsernameLength) { message += `Username must be less than ${this.maxUsernameLength} characters long. `; } if (this.password.length < this.minPasswordLength) { message += `Password must be at least ${this.minPasswordLength} characters long. `; } if (this.password.length > this.maxPasswordLength) { message += `Password must be less than ${this.maxPasswordLength} characters long. `; } return message; }, passwordStrength() { return this.checkPasswordStrength(); }, }, }); </script> <style scoped> /* Style for the form container */ #app { max-width: 400px; margin: 0 auto; padding: 30px; background-color: #fff; border-radius: 10px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } /* Style for the labels */ label { display: block; margin-bottom: 10px; font-weight: bold; color: #333; } /* Style for the inputs */ input[type="text"], input[type="password"] { display: block; width: 100%; padding: 12px; margin-bottom: 20px; border-radius: 5px; border: 1px solid #ccc; background-color: #f2f2f2; font-size: 16px; color: #333; } /* Style for the password strength */ div { margin-top: 10px; font-style: italic; color: #888; font-size: 14px; } /* Style for the button */ button { display: inline-block; padding: 12px 20px; margin-top: 20px; border-radius: 5px; border: none; background-color: #4CAF50; color: #fff; font-size: 16px; cursor: pointer; transition: background-color 0.3s ease; } button:hover { background-color: #3e8e41; } /* Style for the validation message */ p { margin-top: 10px; color: #d50000; font-size: 14px; font-weight: bold; text-align: center; } </style> </body> </html>