Javascript验证函数总是回复无效
Javascript validation function always replying invalid
我已经做了所有正确的事情,但对于所有值,函数仅显示"无效"。有人能告诉我问题出在哪里吗?
<html>
<head>
<script>
function alpha()
{
var x = document.getElementById('input1');
var y = x.value;
var z = isNaN(y);
if(z<1 || z>10){console.log("Invalid");} else {console.log("valid");}
}
</script>
</head>
<body>
<button id="but1" onmouseover="alpha()" style="background:blue;">Click Me</button>
<p id=para1> Hello! This is paragraph One! </p>
<input type=text id=input1 >
</body>
</html>
提前谢谢,很抱歉问了这个愚蠢的问题!但是,我找不到哪里的代码出错了!
isNaN
返回一个布尔值,并将其与数字进行比较。您需要将CCD_ 2与您的范围进行比较。
if(z || x<1 || x>10)
以下是我如何清理你的代码一点
function alpha(){
//name your variables something meaningful
var userInputField = document.getElementById('input1');
//using parse int tells yourself and future programmers you want an int
//it also gives you NaN if it's not a number so you don't have to check yourself
//the 10 is the radix you want to convert to
var userInputedValue = parseInt(x.value,10);
//check explicitly for NaN will save you some headaches
if(userInputedValue === NaN || userInputedValue<1 || userInputedValue>10){
console.log("Invalid");
} else {
console.log("valid");
}
}
相关文章:
- 如果我在javascript中输入无效的电子邮件或空白,如何显示特定的文本框边框红色
- jquery代码在Mozilla中有效,但在其他浏览器上无效
- 这个问号在Flow中意味着什么:“;?()=>“无效”;
- 从字符串末尾删除空白无效
- Jquery Datatables错误:无效的JSON基元:draw
- 滚动在Chrome中有效,但在Firefox或IE中无效
- 回复'js'仅当请求有错误时(否则使用html)
- 错误:语法错误,无法识别的表达式:不支持的伪:无效/RectJS/Unit Testing
- Javascript setattribute-名称和值无效
- 格式化货币无效
- JavaScript错误无效的限定符
- 按键事件无效的原因
- Laravel数据表无效的JSON响应
- 文本字段显示它是有效的,即使它在ExtJS中是无效的
- JS代码在jsbin中有效,在jsfiddle或Chrome/Safari中无效
- 如何使用JavaScript粘贴原始二进制文件而不出现“无效字符”错误
- Xpath对Selenium Web驱动程序无效”;Xpath未找到”;
- Javascript验证函数总是回复无效
- InvokeScript工作,但网页回复“无效参数”
- 来自服务器的无效回复;当使用ajax调用从另一个CFC访问函数时