[英]What's Wrong with Age Range Script?
I am working with the script that will be checking the age from the current date and very simple logic that is by taking year input. 我正在使用将从当前日期开始检查年龄的脚本以及通过输入年份的非常简单的逻辑。 Here is the code:
这是代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Date Check</title>
<script language="javascript" type="text/javascript">
function check()
{
var curtime = new Date;
var curyear = curtime.getFullYear();
var inpyear = document.getElementById('txtdate').value;
if(inpyear.length == 4)
{
var result = curyear - inpyear;
if(result >= 18 || result <= 100)
{
alert('Welcome');
}
else
{
alert('Your Too Much Old or Young to see the Site');
}
}
else
{
alert('Please Put the 4 Digit Year Example : 1987');
}
}
</script>
</head>
<body>
Enter Year: <input type="text" id="txtdate" />
<input type="button" onclick="check();" value="Check" />
</body>
</html>
The Problem is if I enter even 1999 which is less then 18 but still it is giving the message of welcome but it should not true the condition of if because I allowed the range from 18-100. 问题是,即使我输入的年份小于18,但仍然发出欢迎信息,但如果我允许范围为18-100,则它不应该满足if的条件。 Is there any type error or what is the error ?
是否有任何类型错误或错误是什么?
if(result >= 18 && result <= 100)
{
alert('Welcome');
}
result >= 18 || result <= 100
result >= 18 || result <= 100
will always be true. result >= 18 || result <= 100
将始终为true。
result >= 18 || result <100
result >= 18 || result <100
will always be true. result >= 18 || result <100
始终为真。 You have to use &&
您必须使用
&&
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.