cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

            
                <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>
            
          
!
            
              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;
  }
}

            
          
!
            
              // 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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console