In input only numbers on js

A simple JS script, filtering input information in input. Enter only numbers.

<input type="text" onkeypress="filterKey(event);">

function filterKey(k) {
// 48-56 - это [0-9]

if (!(k.keyCode >= 48 && k.keyCode <= 57)){
k.preventDefault();
return false;
}
}

When you press a key in a field, the script checks if it belongs to a range of numbers. If not, it interrupts key entry.

1355 0

Comments

Be the first to review this item. Share your rating and review so that other customers can decide if this is the right item for them.
You have to log in to leave a comment. Sign in

Similar articles

Code highlighting on the site using Js and Css

When describing minimal codes that have a syntax similar to C++, it may be necessary to have minimal highlighting on the site for better readability. I will give the easiest way to highlight, which I took from habrahabr. Consider, connect, describe ...