[Demo] Allow numbers only in HTML input box

This example demonstrates how to prevent special characters and alphabets in an HTML input box. Most important thing is it works in mobile and desktop both.

by Rahul Saraswat

HTML

<input type="tel" class="numeric" maxlength="2" />

JavaScript

$('.numeric').on('input', function (event) { 
    this.value = this.value.replace(/[^0-9]/g, '');
});