Creating an interactive form is only half the battle. The other half involves validating user inputs to ensure they meet certain criteria before the form data is submitted. In this guide, we'll walk you through the steps to create a form and validate its data on the client-side using HTML, CSS, and JavaScript.
Form Setup
Let's consider a simple registration form with the following fields: name
, email
, and password
. The HTML markup would look something like this:
<form id="registrationForm">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br>
<input type="submit" value="Submit">
</form>
Form Validation with JavaScript
The main logic for form validation will reside in a JavaScript function that gets called when the form is submitted. Here's what the skeleton of the function would look like:
document.getElementById('registrationForm').addEventListener('submit', function(event) {
event.preventDefault();
// validation logic goes here
});
We are using the addEventListener
function to listen for the 'submit' event on the form. When the event happens, our function is called. Inside the function, event.preventDefault()
is used to stop the form from being submitted immediately, which gives us a chance to validate the form data.
Validating the Name
Let's add some simple validation for the name
field. We'll check that it's not empty:
var name = document.getElementById('name').value;
if(name === '') {
alert('Name is required.');
return;
}
Validating the Email
For the email
, we'll check that it's not empty and that it's a valid email address. We can use a regular expression to check the format of the email:
var email = document.getElementById('email').value;
if(email === '') {
alert('Email is required.');
return;
} else if (!/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/.test(email)) {
alert('Please enter a valid email address.');
return;
}
Validating the Password
For the password
, we'll check that it's not empty and that it meets certain strength criteria. For instance, we'll require that the password is at least 8 characters long:
var password = document.getElementById('password').value;
if(password === '') {
alert('Password is required.');
return;
} else if (password.length < 8) {
alert('Password must be at least 8 characters long.');
return;
}
Form Validation Feedback
Alerting the user with error messages can disrupt the user experience. It would be better to display the error messages on the form itself. This can be achieved by adding a <span>
element after each input field where the error message for that field will be displayed. We can use CSS to style these error messages:
.error {
color: red;
}
Then in our JavaScript validation code, instead of using alert
, we can set the text content of these <span>
elements:
document.getElementById('nameError').textContent = 'Name is required.';
Conclusion
Client-side form validation is an essential part of creating interactive forms. It ensures that users provide data in the correct format before it's sent to the server, saving resources and enhancing user experience.
If you need professional assistance in web development, don't hesitate to contact our team at GetSmartWebsite. Our website design services are designed to take your web presence to the next level. Stay tuned for more helpful posts like this one, and happy coding!
Top comments (0)