Number Validation with JavaScript

01/11/2021

Demo

Full Screen

Code

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Phone Number Validation with JavaScript</title>
    <link rel="stylesheet" type="text/css" href="https://demo.plantpot.works/assets/css/normalize.css">
    <link rel="stylesheet" href="https://use.typekit.net/opg3wle.css">
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="number_validation.js"></script>
  </head>
  <body>
    <div id="container">
      <form id="form" class="form" name="form" action="#" method="post">
        <div class="form-input-box">
          <label class="form-label" for="phonenumber">Phone number<span class="form-required"> *</span></label>
          <input id="phonenumber" class="form-input" type="tel" name="phonenumber" placeholder="Enter phone number" oninput="number_validation()" required>
          <p id="msg" class="msg"></p>
        </div>
      </form>
    </div>
  </body>
</html>

CSS

@charset "utf-8";
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
html {
  font-size: 16px;
}
body {
  background-color: #f5f6e7;
  font-family: futura-pt, sans-serif;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
}
#container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  width: 100%;
  height: 100vh;
}
.form {
  width: 100%;
  max-width: 450px;
  padding: 20px;
}
.form-input-box {
  position: relative;
}
.form-label {
  display: inline-block;
  margin: .5rem 0;
  color: #3d3935;
  font-size: 1.125rem;
  letter-spacing: .2px;
}
.form-input {
  width: 100%;
  padding: 15px 10px;
  border: 1px solid #3d3935;
  border-radius: 4px;
  background-color: #fff;
  color: #3d3935;
  font-size: 1.125rem;
  outline: 0;
}
.form-input::placeholder {
  color: #ccc;
  font-size: 1.125rem;
}
.form-required {
  color: #bf0404;
}
.msg {
  position: absolute;
  bottom: -30px;
  left: 0;
  font-size: 1.125rem;
}
.valid .form-input {
  border: 1px solid #23b08d;
  background-image: url("valid.svg");
  background-position: 98%;
  background-size: 20px 20px;
  background-repeat: no-repeat;
}
.invalid .form-input {
  border: 1px solid #bf0404;
  background-image: url("invalid.svg");
  background-position: 98%;
  background-size: 20px 20px;
  background-repeat: no-repeat;
}
.valid .msg {
  color: #23b08d;
}
.invalid .msg {
  color: #bf0404;
}

JavaScript

function number_validation() {
  var form = document.getElementById("form");
  var phonenumber = document.getElementById("phonenumber").value;
  var msg = document.getElementById("msg");

  var pattern = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im;

  if(phonenumber.match(pattern)) {
    form.classList.remove("invalid");
    form.classList.add("valid");

    msg.innerHTML = "Phone number is valid.";
  }
  else {
    form.classList.remove("valid");
    form.classList.add("invalid");

    msg.innerHTML = "Phone number is invalid.";
  }
}