+ 4
Is there a keyword for number?
I'm trying to make a calculator, and I want there to be an alert saying that the value has to be a number if what the user typed is not, but I don't know how to say that. Any ideas? https://code.sololearn.com/WS5nmS4omS32/?ref=app
3 odpowiedzi
+ 4
Thank you!
+ 3
There is an event called 'onkeypress'
within that evet you can chek if pressed key is numeric
<!-- html -->
<input type="text" onkeypress="isNumeric(ev)">
// JavaScript
function isNumeric(ev){
if (ev.charCode < 48 && ev.charCode > 57){
alert('only number please');
return false;
}
}
+ 2
The condition isNaN checks to see if the variable is not a number (NaN), and returns true if it is. You can use this in an if statement to check if it is a number by reversing the condition to !isNaN, which runs the statements only if it is a number.
Here's an example code:
https://code.sololearn.com/WaB1277ApdI0/?ref=app