screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://unpkg.com/vue@3.2.12/dist/vue.global.js"></script> </head> <body> <div id="app"> <h3>Vue Js Input mask for time(hh:mm)</h3> <input type='text' v-model="time" @input="formatTime" @blur="validateTime" placeholder="hh:mm" /> <p v-if="!isValidTime" style="color: red;">Invalid time format</p> </div> <script type="module"> const app = Vue.createApp({ data() { return { time: '', isValidTime: true, }; }, methods: { formatTime(event) { let input = event.target.value; input = input.replace(/\D/g, ''); // Remove non-digit characters const match = input.match(/^(\d{0,2})(\d{0,2})$/); // Split input into hours and minutes if (match) { const hours = match[1] || '00'; const minutes = match[2] || '00'; this.time = `${hours}:${minutes}`; } else { this.time = ''; } }, validateTime() { const [hours, minutes] = this.time.split(':'); const isValidFormat = /^\d{2}:\d{2}$/.test(this.time); const isValidTime = isValidFormat && hours >= 0 && hours <= 23 && minutes >= 0 && minutes <= 59; this.isValidTime = isValidTime; }, }, }); app.mount('#app'); </script> <style> #app { width: 400px; margin: 20px auto; box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .24); text-align: center; padding: 20px; } input[type="text"] { padding: 5px; border: 1px solid #ccc; border-radius: 3px; width: 200px; } input[type="text"]:focus { outline: none; border-color: #007bff; } </style> </body> </html>