Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>

<head>
  <title>Simple JavaScript Template</title>
</head>

<body>
  <header>
    <h1>@learnMern</h1>
    <h3>Simple JavaScript Template</h3>
    <p>Everything below this line is created with JavaScript</p>
  </header>

  <div class="container">

    <div class="container container__main">
      <div class="container container__header">
        <i class="fas fa-phone fa-3x"></i>
        <h1>Validator</h1>
      </div>

      <input id="userPhoneNum" type="text" placeholder="Enter phone number">
      <button class="btn btn__primary" onclick="submit()">Validate</button>
      <button class="btn btn__secondary" onclick="clearSubmit()">Clear</button>

      <div id="content" class="content">
        <h3>Phone Number Validation Return</h3>
        <p id="content__phoneNumber"></p>
      </div>
    </div>

  </div>

</body>

</html>
              
            
!

CSS

              
                :root {
  --primary-light: #8abdff;
  --primary: #6d5dfc;
  --primary-dark: #5b0eeb;
  --white: #ffffff;
  --greyLight-1: #e4ebf5;
  --greyLight-2: #c8d0e7;
  --greyLight-3: #bec8e4;
  --greyDark: #9baacf;
}

$shadow: 0.3rem 0.3rem 0.6rem var(--greyLight-2),
  -0.2rem -0.2rem 0.5rem var(--white);
$inner-shadow: inset 0.2rem 0.2rem 0.5rem var(--greyLight-2),
  inset -0.2rem -0.2rem 0.5rem var(--white);

html {
  box-sizing: border-box;

  overflow-y: scroll;
  background: var(--greyLight-1);
  color: var(--primary);
  @media screen and (min-width: 900px) {
    font-size: 75%;
  }
}

h3 {
  color: rgba(109, 93, 252, 0.89);
}

p {
  color: rgba(109, 93, 252, 0.6);
}

header {
  border-bottom: 1px solid rgba(255, 255, 255, 0.6);
}

.container {
  width: 100%;

  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  font-family: "Poppins", sans-serif;
  background: var(--greyLight-1);
  &__main {
    width: 50%;
    min-height: 200px;
    border-radius: 1rem;
    margin: 4rem;
    padding: 2rem;
    box-shadow: 0.8rem 0.8rem 1.4rem var(--greyLight-2),
      -0.2rem -0.2rem 1.8rem var(--white);
  }
  &__header {
    width: 100%;
    justify-content: space-between;
    padding: 2rem;
    box-shadow: $shadow;
    border-radius: 1rem;
    color: rgba(109, 93, 252, 0.89);
  }
}

.content {
  display: none;
  flex-direction: column;
  border-radius: 1rem;
  box-shadow: 0.8rem 0.8rem 1.4rem var(--greyLight-2),
    -0.2rem -0.2rem 1.8rem var(--white);
  padding: 2rem;
  width: 100%;
  margin: 2rem 0;
}

span {
  color: #ff073a;
  font-weight: bold;
}

input {
  width: 100%;
  margin: 2rem 0 1rem 0;
  height: 3rem;
  border: none;
  border-radius: 1rem;
  font-size: 1rem;
  padding-left: 1rem;
  box-shadow: $inner-shadow;
  background: none;
  font-family: inherit;
  color: var(--greyDark);

  &::placeholder {
    color: var(--greyLight-3);
  }
  &:focus {
    outline: none;
    box-shadow: $shadow;

    + .search__icon {
      color: var(--primary);
    }
  }
}

.btn {
  width: 100%;
  height: 3rem;
  margin: 2rem 0 1rem 0;
  font-family: inherit;
  font-size: 1rem;
  border: 0;
  padding: 0;
  border-radius: 1rem;
  box-shadow: $shadow;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: 0.3s ease;

  &__primary {
    background: var(--primary);
    box-shadow: inset 0.2rem 0.2rem 1rem var(--primary-light),
      inset -0.2rem -0.2rem 1rem var(--primary-dark), $shadow;
    color: var(--greyLight-1);
    &:hover {
      color: var(--white);
    }
    &:active {
      box-shadow: inset 0.2rem 0.2rem 1rem var(--primary-dark),
        inset -0.2rem -0.2rem 1rem var(--primary-light);
    }
  }
  &__secondary {
    color: var(--greyDark);
    &:hover {
      color: var(--primary);
    }
    &:active {
      box-shadow: $inner-shadow;
    }
  }
}

@media only screen and (min-width: 768px) {
  input {
    width: 30%;
  }
  .btn {
    width: 20%;
  }

  .container__main {
    justify-content: space-between;
  }

  .btn__primary {
    position: relative;
    left: 10%;
  }
}

              
            
!

JS

              
                // DOM variables
const userPhoneNum = document.getElementById("userPhoneNum");
const content = document.getElementById("content");
const content__phoneNumber = document.getElementById("content__phoneNumber");

// regex variable to match valid 10 digit phone numbers 555-555-5555, (555)555-5555, (555) 555-5555, 555 555 5555, 5555555555, 1 555 555 5555
const regex = /^1?\s?(\(\d{3}\)|\d{3})(\s|-)?\d{3}(\s|-)?\d{4}$/gm;

// logs an explanation of the regex
const regexSummary = (regex) => {
  console.log(`regex: ${regex} \n
    **** ^1? **** matches strings that start with 1 and 1 is optional.
      ^ anchor matches the beginning of the string.
      ? quantifier is for optional\n
    **** \\s? **** followed by a space and space being optional 
      \\s character class matches whitespace; spaces, tabs, and line breaks.\n
    **** (\\(\\d{3}\\) **** followed by open parentheses and three digits followed by closed parentheses. \n
      \\d matches any digit 0-9
      {3} matches three digits \n
    **** | **** or \n
      | acts as a boolean Or and matches the expression befor or after the | \n
    **** \\d{3})(\\s|-)? **** followed by three digits followed by an optional space or hyphen. \n    
    **** | **** or \n
    **** \\d{3}(\\s|-)? **** followed by three digits and an optional space or hyphen. \n
    **** \\d{4}$ **** followed by four digits found at the end of the string. \n
    $ matches the end of the string.

    `);
};

// validates a phone number
const validatePhone = (string) => {
  regexSummary(regex);

  if (string.match(regex)) {
    console.log(true);
    return true;
  } else {
    console.log(false);
    return false;
  }
};

// handler functions
const submit = () => {
  let phoneNumber = userPhoneNum.value;

  if (validatePhone(phoneNumber)) {
    content.style.display = "flex";
    content__phoneNumber.innerHTML = `The phone number <span id="phoneNumber__span">${phoneNumber}</span> is valid!!!`;
    document.getElementById("phoneNumber__span").style.color = "#49fb35";
    userPhoneNum.value = "";
  } else {
    content.style.display = "flex";
    content__phoneNumber.innerHTML = `The phone number <span id="phoneNumber__span">${phoneNumber}</span> is NOT valid!!!, please enter a valid 10 digit number`;
    document.getElementById("phoneNumber__span").style.color = "#ff073a";
    userPhoneNum.value = "";
  }
};

const clearSubmit = () => {
  content.style.display = "none";
  content__phoneNumber.innerHTML = "";
  console.clear();
};

              
            
!
999px

Console