Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
JavaScript Allow Only Numbers?
I am learning a bit on how to code in JavaScript and I just made a basic thing that tells you if the number is prime or not.
Here's the code:
<html>
<body>
<script type="text/javascript">
function calc ()
{
input = document.math_form.user_input.value;
var equ = input / 2
if(equ == Math.round(equ))
{
alert("This number is not prime")
}
else
{
alert("This number is prime")
}
}
</script>
<form name="math_form">
Number:
<input type="text" name="user_input"><br><br>
<input type="button" name="findout" value="Find Out"
onClick="calc ()"><br><br>
Result:
<input type="text" name="result">
</body>
</html>
I was wondering if there is a way to allow only numbers in the input box. Is there some way you can alert the user if it is an invalid number? Thank you
3 Answers
- richarduieLv 61 decade agoFavorite Answer
Your test is not for primality but rather even-ness.
<html>
<head>
<script type="text/javascript">
function calc() {
var input = parseInt( document.math_form.user_input.value );
if (isNaN( input ) {
alert( 'input must be an integer' );
return;
}
var equ = input / 2;
if(equ == Math.round( equ )) {
alert("This number is even")
}
else {
alert("This number is not even")
}
}
</script>
</head>
<body>
<form name="math_form">
Number:
<input type="text" name="user_input" /><br><br>
<input type="button" name="findout" value="Find Out"
onclick="calc()" /><br><br>
Result:
<input type="text" name="result" />
</form>
</body>
</html>
- 1 decade ago
To test for only numbers you would use regular expressions and the test function. For example, a sample number check function would be:
var numRE = /^\d*$/; /* slash d is Regex for numbers only */
function numberCheck(num){
if(numRE.test(num)){
return true;
} else {
return false;
}
}
As far as alerting the user...
The most simple (and annoying) way is when a user leaves the field (onblur) to run the same function above but to alert the user that they need to only enter a number.
var numRE = /^\d*$/; /* slash d is Regex for numbers only */
function numberCheck(num){
if(!numRE.test(num)){
alert('You can only enter a number in this field!');
document.getElementById('user_input').focus();
return false;
}
}
<input type="text" name="user_input" id="user_input" onblur="numberCheck(this.value)">
A better way for the message however, would be to make the field a different color, add a warning icon, and a message saying the same information that would go in an alert. This would be done by using createElement and changing the styles with javascript.
A better way for the javascript itself is to take the events off of the HTML and attach them to an event inside of the Javascript.
See:
- 1 decade ago
Hi!
As far as I am aware (and I may be wrong) it is very difficult to allow only numbers to be entered into a text field. However, the script at the following link will work for validation:
http://wsabstract.com/script/script2/numc.shtml
Important - Whoops! Sorry about that! Didn't thoroughly check through the code to see all the stupid credit notices and rubbish like that. Apologies guys!
I hope this helps!