Pen Settings

HTML

CSS

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

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

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.

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

              
                <h1>Ugh, Cross-Browser <code>number</code> Validation</h1>

<form>
  <label>Try some non-numeric characters here: <input id="number" type="number" step="0.01"></label>
  
  <dl>
    <dt><code>input.value</code>:</dt>
    <dd id="value"></dd>
    <dt><code>validity.valid</code>:</dt>
    <dd id="validity"></dd>
  </dl>
</form>

<p>Browsers differ subtly on what they allow as <em>valid</em> values for HTML5 <code>&lt;input type="number"&gt;</code> fields. Not a big deal because you always validate and sanitize your incoming data on the backend too, right? (hopefully you do)</p>

<p>Unfortunately it’s also hard to add custom fallback JavaScript validation because the spec requires that browsers <a href="https://html.spec.whatwg.org/multipage/input.html#number-state-(type%3Dnumber)"><em>empty</em> the number input’s <code>value</code></a> if the input contains a string that can't be converted to a floating point number! This is a problem because some browsers treat commas as <em>numeric</em> while others don’t, and there’s no way to fix up the <code>value</code> with JS before it’s cleared out internally (as far as I can tell)…</p>

<p>If someone knows how to handle these cases more consistently, or has different test results, I’d love to hear it.</p>

<h2>Cross-Browser Test Results (April 2020)</h2>

<p>Update for 2020: there is more consistency now, with Firefox behaving in line with the decisions made by Safari and pre-Chromium Edge. The new Chromium-powered Edge understandably matches Chrome’s <code>number</code> handling. Assuming you no longer support IE11, that leaves only the two divergent paths to test (Chromium vs Non-Chromium), which at least is better than the state of things in 2018.</p>

<table>
  <tr>
    <th><span hidden>Browser</span></th>
    <th>Allows Non-Numeric Input</th>
    <th>Empties <code>value</code> when Invalid</th>
    <th>Commas are Valid*</th>
    <th>Allows trailing . point</th>
    <th>Validation Styling</th>
    <th>Validation Tooltips</th>
    <th><code>validity.valid</code> Set</th>
  </tr>
  <tr>
    <th scope="row">Chrome / Edge (Chromium)</th>
    <td>No</td>
    <td>Yes</td>
    <td>No, can’t be entered</td>
    <td>Yes (removes from value)</td>
    <td>None</td>
    <td>On submit</td>
    <td>Yes</td>
  </tr>
  <tr>
    <th scope="row">Firefox</th>
    <td>Yes</td>
    <td>If any non-numeric characters (including commas)</td>
    <td>Not valid, but can be entered</td>
    <td>Valid in <code>value</code> but only if 1st .</td>
    <td>Red border</td>
    <td>On submit</td>
    <td>Yes</td>
  </tr>
  <tr>
    <th scope="row">Safari (Desktop / iOS)</th>
    <td>Yes</td>
    <td>If any non-numeric characters (including commas)</td>
    <td>Not valid, but can be entered on desktop</td>
    <td>Valid in <code>value</code> but only if 1st .</td>
    <td>None</td>
    <td>On submit</td>
    <td>Yes</td>
  </tr>
  <tr>
    <th scope="row">Edge (Pre-Chromium)</th>
    <td>Yes</td>
    <td>If any non-numeric characters (including commas)</td>
    <td>Not valid, but can be entered</td>
    <td>Valid in <code>value</code> but only if 1st .</td>
    <td>Red border on submit</td>
    <td>On submit</td>
    <td>Yes</td>
  </tr>
  <tr>
    <th scope="row">IE11</th>
    <td>Yes</td>
    <td>No</td>
    <td>Yes</td>
    <td>Sure, use as many . as you like, <em>almost</em> everything seems to be considered <code>valid</code> input in IE11</td>
    <td>Red border on submit</td>
    <td>On submit, but only for certain cases</td>
    <td>Only <code>false</code> when <code>step</code> is invalidated? Really broken.</td>
  </tr>
</table>

<p><strong>* Note:</strong> commas <em>are</em> numeric in certain regions of the world, as they are often used as “thousands” delimiters and sometimes as the decimal separator symbol — here I’m mostly concerned with the lack of consistency in what can be entered between browsers. I haven’t tested for what happens when your page is specified as being in a language where commas are more significant (e.g. do browsers like Chrome allow them to be entered, then?)</p>
              
            
!

CSS

              
                /* Nothing relevant to see here, just making this pen more readable */
body {
  margin: 2rem;
}

form {
  float: right;
  margin-left: 1rem;
  padding: 2rem;
  background-color: #f0f0f0;
}

p {
  max-width: 50em;
  font-size: 18px;
  line-height: 1.5;
}

label {
  font-weight: bold;
}

input {
  display: block;
  margin-top: .5rem;
  margin-bottom: 1rem;
  width: 100%;
}

dl {
  overflow: hidden;
  margin-bottom: 0;
}

dt, dd {
  float: left;
  clear: right;
}

dt {
  clear: both;
  margin-bottom: .5rem;
}

dd {
  margin-left: .5rem;
}

table {
  border: 1px solid #000;
}

th, td {
  border-bottom: 1px solid #ddd;
  padding: .5rem 1rem;
  vertical-align: top;
}

tr:last-child th,
tr:last-child td {
  border-bottom: 0;
}

th[scope="row"] {
  text-align: right;
}

td {
  text-align: center;
}
              
            
!

JS

              
                var output = document.getElementById('value');
var validity = document.getElementById('validity');

document.getElementById('number').addEventListener('input', function(e) {
  output.innerHTML = e.target.value;
  validity.innerHTML = e.target.validity.valid;
});
              
            
!
999px

Console