Category: javascript
jquery form validation and effects
Published on 30 Sep 2026
Explanation
jQuery can be used to validate form fields before sending data to the server.
Code:
$('#loginForm').on('submit', function(event) {
const username = $('#username').val();
if (!username) {
event.preventDefault();
alert('Username is required');
}
});
Explanation
Check the length of user input before allowing the form submission.
Code:
const password = $('#password').val();
if (password.length < 8) {
alert('Password must contain at least 8 characters');
}
Explanation
jQuery provides show() and hide() methods for displaying and hiding elements.
Code:
$('#message').hide();
$('#message').show();
Explanation
fadeIn(), fadeOut(), slideUp(), and slideDown() provide simple visual effects.
Code:
$('#box').fadeOut(500);
$('#box').fadeIn(500);
$('#menu').slideUp();
$('#menu').slideDown();
Explanation
The toggle() method switches an element between visible and hidden states.
Code:
$('#btn').on('click', function() {
$('#menu').toggle();
});