[英]how to show an alert box In javascript
I've got 2 text-boxes wherein the user has to enter a number.我有 2 个文本框,用户必须在其中输入一个数字。 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?在 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
:如果要检查用户是否输入了数字,可以使用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.其中 inputField 是输入字段的 ID。 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/ JSFiddle: http : //jsfiddle.net/m5cTr/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.