input only numbers javascript code example

Example 1: html restrict input to numbers

<input type="text" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');" />

Example 2: html input box integer only

<input type="number" name="someid" />

Example 3: type numeric value only in textbox javascript

function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}

Example 4: type numeric value only in textbox javascript

<input type="text" class="textfield" value="" id="extra7" name="extra7" onkeypress="return isNumber(event)" />

Tags:

Css Example