Documentation and Books

Recent site activity

Web Development‎ > ‎JavaScript‎ > ‎

How can i check if a textfield only contains digits?

This is fairly simple by using the parseInt JavaScript function in combination with the isNaN function:

<html>
<head>
<script language="javascript">

function validate() {
var element = document.getElementById('myField');
var numberValue = parseInt(element.value);
if (isNaN(numberValue)) {
alert('not a number');
}
}
</script>
</head>
<body>
<input id="myField" type="text" size="4"/>
<input type="button" onclick="validate();" value="validate!!"/>
</body>
</html>