Create Effective Signup Form Validation with HTML, CSS, and JavaScript

Learn how to create a robust signup form with HTML, CSS, and JavaScript. Our step-by-step guide will help you implement form validation like a pro.

create-a-responsive-medical-landing-page-with-html-css-and-javascript.webp

Read Also Create a Responsive Medical Landing Page with HTML, CSS, and JavaScript

Table of Contents

Signup form validation is a crucial aspect of web development. It ensures that the data entered by users is accurate and complete. In this guide, we'll walk you through the process of creating a robust signup form with HTML, CSS, and JavaScript.

Join My Telegram Channel to Download the Project Source Code: Click Here

Prerequisites:

Before starting this tutorial, you should have a basic understanding of HTML, CSS, and JavaScript. Additionally, you will need a code editor such as Visual Studio Code or Sublime Text to write and save your code.

Source Code

Step 1 (HTML Code):

Start by creating the HTML structure for your form. Use the element to define the form, and add input fields for user details such as username, email, and password. Don't forget to include a submit button.

Let's break down each part of the code:

1. : This declaration specifies that the document is an HTML5 document.

2. : This is the opening tag for the HTML document and indicates that the content is in the English language.

3. : This section contains meta information and links to external resources that are important for the page but not directly visible to the user.

4. : This is the main content section of the web page, where visible content is placed.

5. : A container div that wraps the entire content of the page. It is often used for layout and styling purposes.

6. : A div element containing a header with the text "CREATE ACCOUNT."

7. : A form element with the class "form" and the ID "form." This form will contain input fields for user registration.

8. Inside the form, there are several elements, each representing a form field. Each form control div contains:

9. : A submit button within the form. Clicking this button will trigger the form submission.

10. : A reference to an external JavaScript file named "script.js." This file contains client-side scripting, which may be used for form validation or other dynamic functionality on the page.

Read Also Simple Steps to Create a Draggable HTML Element with JavaScript (Source code)

Step 2 (CSS Code):

Make your form visually appealing and user-friendly with CSS. Apply styles to form elements, buttons, and error messages. This step ensures that your form is not only functional but also aesthetically pleasing.

Let's break down the code step by step:

1. * Selector:

2. body Selector:

3. .container Selector:

4. .header Selector:

5. .header h2 Selector:

6. .form Selector:

7. .form-control Selector:

8. .form-control label Selector:

9. .form-control input Selector:

10. .form-control i Selector:

11. .form-control small Selector:

12. .form button Selector:

13. .form-control.success and .form-control.error Selectors:

* < box-sizing: border-box; >body < background-color: #8e89cf; display: flex; align-items: center; justify-content: center; min-height: 100vh; margin: 0; >.container < background-color: #fff; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.3); width: 400px; max-width: 100%; overflow: hidden; >.header < background-color: #f7f7f7; border-bottom: 1px solid #f0f0f0; padding: 20px 40px; >.header h2 < margin: 0; >.form < padding: 30px 40px; >.form-control < margin-bottom: 10px; padding-bottom: 20px; position: relative; >.form-control label < display: inline-block; margin-bottom: 5px; >.form-control input < border:2px solid #f0f0f0; border-radius: 4px; display: block; font-size: 14px; width:100%; padding:10px; >.form-control i < position: absolute; top: 40px; right: 10px; visibility: hidden; >.form-control small < position: absolute; bottom: 0; left: 0; visibility: hidden; >.form button < background-color: #8e44fd; border:2px solid #8e44fd; color: #fff; display: block; padding: 10px; width:100%; font-size: 16px; border-radius: 4px; >.form-control.success input < border-color: #2ecc71; >.form-control.error input < border-color: #e74c3c; >.form-control.success i.fa-check-circle < visibility: visible; color: #2ecc71; >.form-control.error i.fa-exclamation-circle < visibility: visible; color: #e74c3c; >.form-control.error small

Step 3 (JavaScript Code):

Implement basic form validation using JavaScript. Check if required fields are filled, validate email addresses, and ensure that passwords meet specific criteria. Use simple conditional statements for this purpose.

Let me break it down step by step:

1. The code begins by selecting several HTML elements by their IDs and storing them in variables:

2. An event listener is added to the form element, which listens for the "submit" event. When the form is submitted, it prevents the default form submission behavior using e.preventDefault() and calls the checkInputs() function.

3. The checkInputs() function is defined to perform input validation. It does the following for each input field:

4. The setError(input, msg) function is used to display error messages and apply an "error" class to the parent element of the input field. It takes two parameters:

5. The setSuccess(input) function is used to apply a "success" class to the parent element of the input field to indicate that the input is valid. It takes one parameter:

6. The isemail(email) function is used to validate the email address format. It uses a regular expression to check if the provided email is in a valid format. If the email format is invalid, it returns false, and if it's valid, it returns true.

7. Finally, the code uses various conditional statements to check the validity of the username, email, password, and confirm password fields. If any of these fields are empty or don't meet specific criteria, error messages are displayed, and the "error" class is applied. If the fields are valid, the "success" class is applied.

const form=document.getElementById("form"); const username=document.getElementById("username"); const email=document.getElementById("email"); const password=document.getElementById("password"); const confirm=document.getElementById("confirm"); form.addEventListener("submit",(e)=>< e.preventDefault(); checkInputs(); >); function checkInputs() < // to prevent from spaces we use trim const usernamevalue=username.value.trim(); const emailvalue=email.value.trim(); const passwordvalue=password.value.trim(); const confirmvalue=confirm.value.trim(); if(usernamevalue === '')< // add error class setError(username,"This field cannot be blank . "); >else < // add success class setSuccess(username); >if(emailvalue === '') < // add error class setError(email,"This field cannot be blank . "); >else if (!isemail(emailvalue)) < setError(email,"Check the email dude . "); >else < // add success class setSuccess(email); >if(passwordvalue === '')< // add error class setError(password,"This field cannot be blank . "); >else if(passwordvalue.length <=4)< setError(password,"Password is too small!!") >else < // add success class setSuccess(password); >if(confirmvalue === '')< // add error class setError(confirm,"This field cannot be blank . "); >else if(passwordvalue !== confirmvalue) < setError(confirm,"Dude ! Password and Confirm Password must be same.") >else < // add success class setSuccess(confirm); >> function setError(input,msg) < const formControl=input.parentElement; const small=formControl.querySelector("small"); small.innerText=msg; // add error class formControl.className="form-control error"; >function setSuccess(input) < const formControl=input.parentElement; formControl.className="form-control success"; >function isemail(email)< return /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]\.[0-9]\.[0-9]\.[0-9]])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]))$/.test(email); >
Read Also Create Drag and Drop File Upload with HTML, CSS and jQuery

Congratulations! You've mastered the art of signup form validation. By following these steps and best practices, you can create a secure and user-friendly registration process for your website. Form validation is a crucial part of web development, and you now have the skills to implement it effectively. Happy coding!

I hope you enjoyed this post. Now, with these examples, you can create your own amazing page.

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!