Form Events
Handle form submission and input events
BeginnerTopic: Events Programs
JavaScript Form Events Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Method 1: Form submission
const form = document.getElementById('myForm');
form.addEventListener('submit', function(e) {
e.preventDefault(); // Prevent default submission
console.log('Form submitted');
const formData = new FormData(form);
console.log('Form data:', Object.fromEntries(formData));
});
// Method 2: Input events
const input = document.getElementById('myInput');
input.addEventListener('input', function(e) {
console.log('Input value:', e.target.value);
});
input.addEventListener('change', function(e) {
console.log('Value changed:', e.target.value);
});
input.addEventListener('focus', function(e) {
console.log('Input focused');
e.target.style.borderColor = 'blue';
});
input.addEventListener('blur', function(e) {
console.log('Input blurred');
e.target.style.borderColor = '';
// Validate on blur
validateInput(e.target);
});
// Method 3: Real-time validation
input.addEventListener('input', function(e) {
const value = e.target.value;
if (value.length < 3) {
e.target.setCustomValidity('Must be at least 3 characters');
} else {
e.target.setCustomValidity('');
}
});
// Method 4: Select element
const select = document.getElementById('mySelect');
select.addEventListener('change', function(e) {
console.log('Selected:', e.target.value);
});
// Method 5: Checkbox
const checkbox = document.getElementById('myCheckbox');
checkbox.addEventListener('change', function(e) {
console.log('Checked:', e.target.checked);
});
// Method 6: Radio buttons
const radios = document.querySelectorAll('input[type="radio"]');
radios.forEach(radio => {
radio.addEventListener('change', function(e) {
if (e.target.checked) {
console.log('Selected:', e.target.value);
}
});
});
// Method 7: Textarea
const textarea = document.getElementById('myTextarea');
textarea.addEventListener('input', function(e) {
const length = e.target.value.length;
console.log('Character count:', length);
if (length > 100) {
e.target.style.color = 'red';
} else {
e.target.style.color = '';
}
});
// Method 8: Form validation
form.addEventListener('submit', function(e) {
e.preventDefault();
const email = form.querySelector('#email').value;
const password = form.querySelector('#password').value;
if (!email.includes('@')) {
alert('Invalid email');
return;
}
if (password.length < 8) {
alert('Password must be at least 8 characters');
return;
}
console.log('Form is valid');
// Submit form
});
// Method 9: Reset event
form.addEventListener('reset', function(e) {
console.log('Form reset');
});
// Method 10: Invalid event
input.addEventListener('invalid', function(e) {
e.preventDefault();
console.log('Invalid input');
showError(e.target, 'Please enter a valid value');
});Output
// Output depends on form interaction
Understanding Form Events
Form events handle user input and submission.
Event Types
•submit: Form submitted
•reset: Form reset
•input: Value changed (real-time)
•change: Value changed (on blur)
•focus: Element focused
•blur: Element blurred
•invalid: Validation failed
Form Elements
•input: Text, email, password, etc.
•textarea: Multi-line text
•select: Dropdown
•checkbox: Boolean input
•radio: Single choice
Validation
•HTML5 validation
•Custom validation
•Real-time feedback
•Error messages
Best Practices
•Prevent default on submit
•Validate on blur
•Show errors clearly
•Use FormData for collection
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.