cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

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.

            
              <div class="container">
<div class="row">
  <h1>HTML5 Form Validation Patterns</h1>

  <div class="description note">
    <h3>Validation</h3>
    <p>HTML5 adds JavaScript free, client-side form-validation that can help ensure that input text conforms to a specified format. </p>
    <p>On the input elements we add the <code>pattern</code> attribute and use <a href="https://www.w3schools.com/jsref/jsref_obj_regexp.asp" target="_blank">Regular Expressions</a> to specify the pattern. </p>

  </div>
</div>

  <div class="row">
  <div class="description">
    <h3>London phone number following the pattern <code>+44-20-1234-1234</code></h3>
    <p>This is a pattern that requires a set of defined characters at the start – <code>+44-20-</code> – and then two sets of four digits separated by a dash.</p>
    <p>Because special characters like '+' and '-' might be misinterpreted we put them in square brackets and precede them with a '\' - e.g. <code>[\+]</code>.</p>

    <p>To only accept digits we use <code>[0-9]</code>and to specify the amount of digits we want we use curly brackets containing the number – <code>{4}</code>.</p>
    <p>To accept four digits we use <code>[0-9]{4}</code>.</p>

    <p>The pattern we use here is <code>[\+]44[\-]20[\-][0-9]{4}[\-][0-9]{4}</code> </p>


    <form action="">
      <div class="form-group">


        <label for="phone"></label>
        <input type="tel" class="form-control input-lg" id="phone" placeholder="Enter phone number" pattern="[\+]44[\-]20[\-][0-9]{4}[\-][0-9]{4}" required>
      </div>

      <button class="btn btn-primary">Submit</button>
      <small>Will only submit if valid</small>

    </form>
  </div>
  <!-- end "description" -->
</div>
  
  <div class="row">
  <div class="description">
    <h3>US Passport Number following the pattern <code>A1234567</code></h3>

    <p>To specify upper case letters we use <code>[A-Z]</code>and to specify the amount of letters we want we use curly brackets containing the number – <code>{1}</code>.</p>
    <p>To accept one letter we use <code>[A-Z]{1}</code>.</p>

    <p>The pattern we use here is <code>[A-Z]{1}[0-9]{7}</code>. </p>


    <form action="">
      <div class="form-group">


        <label for="phone"></label>
        <input type="text" class="form-control input-lg" id="phone" placeholder="Enter passport number" pattern="[A-Z]{1}[0-9]{7}" required>
      </div>

      <button class="btn btn-primary">Submit</button>
      <small>Will only submit if valid</small>

    </form>
  </div>
  <!-- end "description" -->
</div>
  
  <div class="row">
  <div class="description note">
    <h3>Note on Shorthand</h3>
    <p><code>[0-9]</code> can be replaced by the shorthand <code>\d</code></p>
    <p><em>Using the shorthand our first example would be <code>[\+]44[\-]20[\-]\d{4}[\-]\d{4}</code></em> </p>
    <p><code>[A-Z]</code> can be replaced by the shorthand <code>\w</code></p>
    <p><em>Using the shorthand our second example would be <code>\w{1}\d{7}</code></em> </p>
    <p>If spaces are required use the shorthand <code>\s</code></p>
    <p>Check out <a href="http://regexr.com/" target="_blank">RegExr</a> for more examples.</p>

  </div>
  <!-- end "description" -->
</div>

</div>
            
          
!
            
              $main-color: #F6F6F6;

body {
  background: darken($main-color, 5%);
  color: darken($main-color, 30%);

}

.description {
  background: lighten($main-color, 15%);
  padding: .7em;
  margin: .5em 0 0 0;
}

.note {
  background: lighten($main-color, 5%);
}

em {
  margin: 0 0 0 .3em;
}

h1, h3 {
  color: darken($main-color, 45%);
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console