JavaScript validation is a way to check whether the data a user enters in a web form is correct before it’s submitted to a server. This helps improve user experience and reduces server load by preventing invalid data from being sent.
JavaScript validation checks form inputs, like ensuring:
• Required fields aren’t left empty.
• Email addresses have the correct format (like “example@domain.com”).
• Passwords meet security rules (e.g., minimum length).
• Numbers fall within a specific range.
• Specific patterns are followed (e.g., phone numbers).
Why is it important ?
• Prevents Errors: By catching mistakes like missing information or invalid formats, JavaScript can stop the form from submitting until the user fixes the errors.
• Improves User Experience: Immediate feedback on what needs fixing helps users understand and correct issues faster.
• Reduces Server Load: If forms are validated on the client side, fewer invalid submissions reach the server, saving resources.
How does it work?
we use event listners to validate the form when a user tries to submit it. Here’s basic example.
Activity 01
Write a JavaScript to include the following validations for the data input through the form given below:
The ‘User Name’ field should not be empty.
The ‘password’ should be more than 6 characters
Display suitable messages to the user based on the result of each validation.