I've got 2 text-boxes wherein the user has to enter a number. If the user enters a letter, an alert box should appear, indicating that only numbers are to be entered.
How can that be done in JavaScript?
Updates:
This is the code I used for the alert box:
function checkIfNumeric(total) {
if (isNaN(total)) {
alert ("not a number!");
}
}
If you want to check if the user has entered a number or not, you could use isNaN
:
function foo() {
var a = document.getElementById("inputField").value;
if (isNaN(a))
{
alert("Must input numbers");
}
else
{
alert("its a number");
}
}
where inputField is the ID of your input field. Of course you can use
if (isNaN(a))
{
alert("Must input numbers");
}
else
{
alert("its a number");
}
in any context, you don't have to place it in an apart function like I did here. JSFiddle: http://jsfiddle.net/m5cTr/1/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.