How to limit the textarea to only hold numbers?
If you use jQuery, you can do it like in this jsfiddle
$('input').keypress(function(e) {
var a = [];
var k = e.which;
for (i = 48; i < 58; i++)
a.push(i);
if (!(a.indexOf(k)>=0))
e.preventDefault();
});
Use HTML5 input number type instead and leverage native browser validation. Thus, no JavaScript required.
<input type="number" />
It has wide support in browsers and it's the standard and best-practice solution.
Also on mobile
and tablets
the numeric key pad will appear instead.
See demo
you need some java script to implement this
<html>
<head>
<title>Test</title>
<script language="javascript">
function checkInput(ob) {
var invalidChars = /[^0-9]/gi
if(invalidChars.test(ob.value)) {
ob.value = ob.value.replace(invalidChars,"");
}
}
</script>
</head>
<body>
<input type="text" onkeyup="checkInput(this)"/>
</body>
</html>