如何检查输入是否是js中的数字

时间:2016-04-26 03:40:17

标签: javascript

这是我一直在努力的代码。

var storeUsersInfo = [];
var amountOfUsers = prompt("How many users do you want?");
amountOfUsers = parseInt(amountOfUsers);
function returnUserInput() {
    var askFirstName = prompt("What is your first name?");
    var askLastName = prompt("What is your last name" + " " +       titleCase(askFirstName) + "?");
    var askAge = prompt("How old are you" + " " + titleCase(askFirstName) +  " " + titleCase(askLastName) + "?");

        if(askAge != int) {
            alert("Not a valid input")

        };

    return {
        firstName: titleCase(askFirstName),
        lastName: titleCase(askLastName),
        age: askAge
    };
};
function titleCase(string) {
    return string.charAt(0).toUpperCase() + string.slice(1); 
};

for(var i = 0; i < amountOfUsers; i++) {
    storeUsersInfo[i] = returnUserInput();
}
console.log(storeUsersInfo);

我想知道如何检查askAge的输入以查看它是否等于数字。

我尝试了一些代码,你可以在第9-12行看到,我无法弄清楚。我知道它必须用typeof做点什么。

思想?

6 个答案:

答案 0 :(得分:1)

将它乘以1,如果它返回NaN并且与原始输入不同 - 它不是数字

        var askAge = prompt("How old are you" + " " + titleCase(askFirstName) +  " " + titleCase(askLastName) + "?");
        var askedAge=parseInt(askAge)*1;
          if(askedAge != askAge) {
              alert("Not a valid input");
            }

答案 1 :(得分:1)

这可以使用Number.isIntegerNumber.parseInt的组合来解决。这两个都已在ES2015标准化。

以下表达式将检查年龄是否有效:

Number.isInteger(Number.parseInt(askAge));

请注意,您必须先将用户输入解析为整数;这可以产生有效的整数,也可以产生NaN

如果是整数,那么Number.isInteger将表达true;否则,解析的数字为NaN,表达式将变为false

答案 2 :(得分:0)

我想你可以使用这段代码,

function isNumeric(x) {
  return !isNaN(parseFloat(x)) && isFinite(x);
}

答案 3 :(得分:0)

答案 4 :(得分:0)

  

试试这个

var check = /^[0-9]+$/;
var checkall = askAge.match(check);
if (!checkall){ 
                 alert("Not a valid input")
       }

答案 5 :(得分:0)

这个神秘的答案:

var askAge = prompt("How old are you" + " " + titleCase(askFirstName) +  " " + titleCase(askLastName) + "?");
if(!Number.isInteger(Number.parseInt(askAge))) {
    alert("Not a valid input")
};

更多

您的假设部分正确,您必须检查数字的类型以确保它是一个数字,并且它是一个整数。你实际上如何做有几种选择。这是一个应该工作的。

您必须确定是否要使用Number()Number.parseInt(),因为此处的确定会对可接受的值产生影响。

鉴于此,我选择在此答案中使用parseInt。我还构造它不接受0作为用户数量的值。

首先我们在isInteger中使用或创建parseIntNumber

Number.isInteger = Number.isInteger || function(value) {
  return typeof value === "number" && isFinite(value) && Math.floor(value) === value;
};
Number.parseInt = Number.parseInt || parseInt;

声明我们的其他功能:(评论)

// this will error out if "Cancel" on the prompt is clicked (null is passed in mystring)
function titleCase(mystring) {
  return mystring.charAt(0).toUpperCase() + mystring.slice(1);
}

function returnUserInput() {
  var isValid = false;
  var askFirstName = "";
  var askLastName = "";
  var askAge = null;
  do {
    askFirstName = prompt("What is your first name?"); // currently accepts " " as a valid name
    askLastName = prompt("What is your last name" + " " + titleCase(askFirstName) + "?"); // accepts " " as a valid name
    askAge = prompt("How old are you" + " " + titleCase(askFirstName) + " " + titleCase(askLastName) + "?");
    isValid = Number.isInteger(Number.parseInt(askAge));
    if (!isValid) {
      alert("Not a valid input");
    }
  } while (!isValid); // accepts 0 and negative values as age
  return {
    firstName: titleCase(askFirstName),
    lastName: titleCase(askLastName),
    age: Number.parseInt(askAge) // was using the unparsed string "4' if 4 was entered
  };
}

使用功能:

var storeUsersInfo = [];
var amountOfUsers = 0;
var enteredCount = 0;
do {
  amountOfUsers = prompt("How many users do you want?");
  enteredCount = Number.parseInt(amountOfUsers, 10);// parse it
} while (!(Number.isInteger(Number.parseInt(amountOfUsers, 10))) && !enteredCount > 0)

amountOfUsers = enteredCount;

for (var i = 0; i < amountOfUsers; i++) {
  storeUsersInfo[i] = returnUserInput();
}
console.log(storeUsersInfo);// console.dir might also work (better?)