Created
August 3, 2023 17:40
-
-
Save saharshg29/0b97ca01efbae23c3806afb0dc728775 to your computer and use it in GitHub Desktop.
Login validation using js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function validateLoginForm() { | |
const usernameInput = document.getElementById('username'); | |
const passwordInput = document.getElementById('password'); | |
const username = usernameInput.value.trim(); | |
const password = passwordInput.value.trim(); | |
if (username === '' || password === '') { | |
alert('Please fill in all fields.'); | |
return false; | |
} | |
return true; | |
} | |
const loginForm = document.getElementById('login-form'); | |
loginForm.addEventListener('submit', function(event) { | |
if (!validateLoginForm()) { | |
event.preventDefault(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment