INPUT pre vloženie čísla
JavaScript -> Ukážkové programy v JavaScripte -> INPUT pre vloženie čísla
Ak chcete vytvoriť <FORM> s položkou <INPUT>, do ktorej bude môcť užívateľ vložiť len číslo, budete si musieť pomôcť s JavaScriptom.
<html>
<body>
<script type="text/javascript">
function onlyNumbers(e)
{
var keynum
var keychar
var numcheck
if (window.event) // IE
{
keynum = e.keyCode
}
else if (e.which) // Netscape/Firefox/Opera
{
keynum = e.which
}
keychar = String.fromCharCode(keynum)
numcheck = /\d/
return numcheck.test(keychar)
}
</script>
<form>
<input type="text" onkeypress="return onlyNumbers(event)" />
</form>
</body>
</html>
<body>
<script type="text/javascript">
function onlyNumbers(e)
{
var keynum
var keychar
var numcheck
if (window.event) // IE
{
keynum = e.keyCode
}
else if (e.which) // Netscape/Firefox/Opera
{
keynum = e.which
}
keychar = String.fromCharCode(keynum)
numcheck = /\d/
return numcheck.test(keychar)
}
</script>
<form>
<input type="text" onkeypress="return onlyNumbers(event)" />
</form>
</body>
</html>