javascript – What’s Wrong with Age Range Script? – Education Career Blog

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. Is there any type error or what is the error ?

,

if(result >= 18 && result <= 100)
    {
        alert('Welcome');
    }

result >= 18 || result <= 100 will always be true.

,

result >= 18 || result <100 will always be true. You have to use &&

Leave a Comment