Javascript: How to check is valid number or not

February 24, 2011 | In: javascript, web development

Numeric validation require in mostly cases when we want that user enter only numeric value in any text field. For example ‘Age’ field. Obviously, age can not be in string format.

<script type="text/javascript">
	function checkNumericValueValidation()
	{
		var enteredValue = document.getElementById('numeric_value').value;
		if (enteredValue == '' || isNaN(enteredValue)) { 
			alert('Ohh! You entered wrong number format.');
		} else {
			alert('Congrates! You entered correct Numeric Value.');
		}
	}
</script>

Above, I have mentioned javascript function to check that entered value is numeric or not.

<strong>Please enter only numeric value :</strong> 
<input type="text" name="numeric_value" id="numeric_value" size="10" />
<input type="button" name="checkvalidation" value="Check Validate Number" onclick="checkNumericValueValidation()" />