在JavaScript中验证十进制数的最干净,最有效的方法是什么?
奖励积分:
测试用例:
01. IsNumeric('-1') => true
02. IsNumeric('-1.5') => true
03. IsNumeric('0') => true
04. IsNumeric('0.42') => true
05. IsNumeric('.42') => true
06. IsNumeric('99,999') => false
07. IsNumeric('0x89f') => false
08. IsNumeric('#abcdef') => false
09. IsNumeric('1.2.3') => false
10. IsNumeric('') => false
11. IsNumeric('blah') => false
Run Code Online (Sandbox Code Playgroud) 我正在用 HTML 和 JS 创建一个示例 MabLibs 类型的东西。当这个人在一个字段中输入内容时,它将使用它来创建他们自己的 MadLib。
我做了一些研究,但没有找到我正在寻找的东西。假设一个人在姓名字段中输入 12。将如何编码,以便如果此实例确实发生,它不会通过并警告“这不是有效输入。请再次输入!” 或类似的规定。
我正在使用的代码如下。我对 Javascript 很陌生,所以我知道格式和内容可能是错误的。
<html><head>
<title>
Mad Libs Story
</title>
<script>
function getVars() {
person1 = String(document.getElementById("personOne").value);
age = Number(document.getElementById("ageOne").value);
firstAdjective = String(document.getElementById("adjective").value);
document.getElementById("madLibCreation").innerHTML = "There once was a person named " + person1 + ". She was " + age + " and very " + firstAdjective = ".";
}
</script>
</head>
<body>
<h3>
Welcome to Mad Libs! Please type in the prompted Information. Then press the submit button. …Run Code Online (Sandbox Code Playgroud)