Лучше на keydown проверять
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
</style>
</style>
</head>
<body>
<input type=text id=digonly>
</body>
<script>
document.getElementById('digonly').addEventListener('keydown',
(ev) => {if( ev.key.length==1 && (ev.key<'0' || '9'< ev.key)) ev.preventDefault() })
</script>
</html>