2

<html>
<head>
<script lang="Javascript">
function validateExpDate(){
    var expdate ="24-10-2018";
    var fields = expdate.split('-');
    var date = parseInt(fields[0]);
    var month = parseInt(fields[1]);
	var year = parseInt(fields[2]);
	var today = new Date();
    var dd = parseInt(today.getDate());
    var mm = parseInt(today.getMonth()+1); //January is 0!
    var yyyy = parseInt(today.getFullYear());
	if(year >= yyyy){
		if(month >= mm){
			if(date > dd){
				alert('Valid Expiry Date');
			}
		}	
	}else{
	alert('Invalid Expiry Date Entered!');
	}
}
</script>
</head>
<body onload="validateExpDate();">
</body>
</html>

I want to compare the date in variable expdate with current date and display appropriate message. Can anyone figure out the problem and make it work?

3
  • Debugging tip - you can put console.log(year, yyyy, month, mm, date, dd) before your if to see the values you'd be comparing. You can also use the debugger to look at them and step through the execution to see what, if anything, is wrong. Commented Oct 30, 2018 at 7:13
  • all values are coming. It's just comparison is not happening and it's frustrating. Commented Oct 30, 2018 at 7:30
  • The comparison is happening. Think about it, if month >= mm or date > dd fails, then where does the code go? Commented Oct 30, 2018 at 7:32

3 Answers 3

1

Your else instruction belongs to the outer if statement. In your case the if statement returns true (2018 >= 2018) so the program jumps to the code inside of the first if-clause. From then on your else statement is no longer reachable for the program.

You are looking for something like this:

<html>
<head>
<script lang="Javascript">
function validateExpDate(){
    var expdate ="24-10-2018";
    var fields = expdate.split('-');
    var date = parseInt(fields[0]);
    var month = parseInt(fields[1]);
    var year = parseInt(fields[2]);
    var today = new Date();
    var dd = parseInt(today.getDate());
    var mm = parseInt(today.getMonth()+1); //January is 0!
    var yyyy = parseInt(today.getFullYear());
    if(year >= yyyy && month >= mm && date > dd)
    {
        alert('Valid Expiry Date');
    }
    else{
    alert('Invalid Expiry Date Entered!');
    }
}
</script>
</head>
<body onload="validateExpDate()">
</body>
</html>

You have all conditions in one if clause. By using the AND operator this statement returns only true if all conditions are true. If one of them is false the program will go inside the else clause.

It is also possible to compare two dates directly in javascript. Just a little less code...

<html>
    <head>
    <script lang="Javascript">
    function validateExpDate(){
        var expdate = new Date('2018-10-24');
        var today = new Date();
        if(expdate > today) {
            alert('Valid Expiry Date');
        }
        else {
            alert('Invalid Expiry Date Entered!');
        }
    }
    </script>
    </head>
    <body onload="validateExpDate()">
    </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

You are not handling the else conditions for date and month. Either you can write code like below or you can use && in first IF loop if that suits you.

if (year >= yyyy) {
                if (month >= mm) {
                    if (date > dd) {
                        alert('Valid Expiry Date');
                    } else {
                        alert('Invalid Expiry Date Entered!');
                    }
                }
                else {
                    alert('Invalid Expiry Date Entered!');
                }
            } else {
                alert('Invalid Expiry Date Entered!');
            }

Comments

0

Thanks guys. I figured out the problem was just curly brasis. Thanks for the effort of replying. Really Appreciated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.