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

              
                  <form>
  <div>
    <label for="email">Email</label>
    <input id="email" type="email" required placeholder="example@example.com" />
  </div>
  <div>
    <label for="alpha">Alphanumeric Only</label>
    <input id="alpha" type="text" pattern="[a-zA-Z0-9]+" title="Only alphanumeric characters allowed" required/>
  </div>
    <div>
    <label for="select">Select</label>
    <select required>
      <option value="">Select option</option>
      <option value="1">Option one</option>
    </select>
  </div>
  <div>
    <label for="password">Password</label>
    <input id="password" type="password" required/>
  </div>
  <input type="submit" value="Log In" />
</form>

<p><strong>Goal:</strong></p>
<ol>
  <li>Only validate inputs on submit
  <li>Leverage browser-level feedback
  <li>Allow input styling off of <code>.error</code> class because <code>input:invalid</code> sorta sucks and triggers on page load.
  <li>Optional: Trigger validation on blur.
</ol>
<p><code>// TODO:</code> Mess around with <code>pattern</code> validation and maybe <code>setCustomValidity(message)</code>.</p>
<p>Tested in Chrome, Firefox, Edge, and Mobile Safari</p>
              
            
!

CSS

              
                body {
  padding: 2em;
  line-height: 1.5;
}

div {
  margin-bottom: 1em;
}

label {
  margin-bottom: 0.25em;
  display: block;  
}

// Never Style input:invalid, it sucks. It triggers way too early. Uncomment to see.
// input:invalid {
//   border-color: red;
// }

// Applying an error class means we keep browser level feedback, error handling, and focus management.
input.error,
select.error,
textarea.error {
  outline: none;
  border-color: red;
  border-width: 1px;
  border-style: solid;
  
  // Use CSS to visually restore valid fields
  &:valid {
    border-style: auto;
    border-color: inherit;
  }
}

              
            
!

JS

              
                // Doing error handling on form submit won't work here because the validation blocks the submit event from firing.

//Get all the inputs...
const inputs = document.querySelectorAll('input, select, textarea');

// Loop through them...
for(let input of inputs) {
  // Just before submit, the invalid event will fire, let's apply our class there.
  input.addEventListener('invalid', (event) => {
    input.classList.add('error');    
  }, false);
  
  // Optional: Check validity onblur
  input.addEventListener('blur', (event) => {
    input.checkValidity();
  })

}

              
            
!
999px

Console