Form Validation using JS Regular Expressions

Web Design

In this tutorial we are goin to create a registration form in HTML and CSS and also use materialize css classes. In order to validate the form we are going to use JavaScript regular expressions to validate name input, phone number input validation, zip code input validation, password input validation, email input validation. After all of the input fields are valid we will submit the form.

Source Code:


<h1>Form Validation using RegEx</h1>

    <div class="form=container">
      <form class="col s12">
        <div class="input-container">
          <label for="FullName">Name</label>
          <input placeholder="Enter Your Full Name" id="FullName" type="text" />
        <div class="input-container">
          <label for="Phone">Phone</label>
          <input id="Phone" type="text" />

        <div class="input-container">
          <label for="zipCode">Zip Code</label>
          <input id="zipCode" type="text" />

        <div class="input-container">
          <label for="Password">Password</label>
          <input id="Password" type="password" />

        <div class="input-container">
          <label for="Email">Email</label>
          <input id="Email" type="email" />
        <input type="submit" value="Submit" class="btn red" />


input.notValid {
  color: red !important;
  border-bottom: 1px solid red !important;


// Todo: get DOM elements and add EventListeners to them
// get Elements
const fullName = document.getElementById("FullName");
const phone = document.getElementById("Phone");
const zipCode = document.getElementById("zipCode");
const eMail = document.getElementById("Email");
const password = document.getElementById("Password");

// add EventListeners
fullName.addEventListener("blur", validateName);
phone.addEventListener("blur", validatePhone);
zipCode.addEventListener("blur", validateZipCode);
eMail.addEventListener("blur", validateEmail);
password.addEventListener("blur", validatePassword);

Useful resources:

Mozila develope network


1 thought on “Form Validation using JS Regular Expressions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.