How can I validate an expiry date in Javascript?

I’m a beginner to HTML, Javascript, and coding in general. For an assessment, I’m required to create a form with HTML, and validate it with a Javascript file. If an inputted answer isn’t valid, it should return false. For a credit card, a user is required to input an expiry date, and if the date has already passed, it should return false. Though despite this, no matter how much I troubleshoot, it always returns true with no errors. Any help would be appreciated!

HTML

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><form name="userForm" onsubmit="return validateForm()">
<fieldset>
<legend>Payment details</legend>
<div>
<lable for="exMonth">Expiry MM/YY</lable>
<select id="exMonth" name="exMonth">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<select id="exYear" name="exYear">
<option value="2024">2024</option>
<option value="2025">2025</option>
<option value="2026">2026</option>
<option value="2027">2027</option>
<option value="2028">2028</option>
</select>
</div>
</fieldset>
<br>
<input type="submit">
<button>Help</button>
<p id="demo"></p>
<script src="../js/assessment1.js">
</script>
</code>
<code><form name="userForm" onsubmit="return validateForm()"> <fieldset> <legend>Payment details</legend> <div> <lable for="exMonth">Expiry MM/YY</lable> <select id="exMonth" name="exMonth"> <option value="01">01</option> <option value="02">02</option> <option value="03">03</option> <option value="04">04</option> <option value="05">05</option> <option value="06">06</option> <option value="07">07</option> <option value="08">08</option> <option value="09">09</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> </select> <select id="exYear" name="exYear"> <option value="2024">2024</option> <option value="2025">2025</option> <option value="2026">2026</option> <option value="2027">2027</option> <option value="2028">2028</option> </select> </div> </fieldset> <br> <input type="submit"> <button>Help</button> <p id="demo"></p> <script src="../js/assessment1.js"> </script> </code>
<form name="userForm" onsubmit="return validateForm()">
        <fieldset>
            <legend>Payment details</legend>
                <div>
                    <lable for="exMonth">Expiry MM/YY</lable>
                    <select id="exMonth" name="exMonth">

                        <option value="01">01</option>
                        <option value="02">02</option>
                        <option value="03">03</option>
                        <option value="04">04</option>
                        <option value="05">05</option>
                        <option value="06">06</option>
                        <option value="07">07</option>
                        <option value="08">08</option>
                        <option value="09">09</option>
                        <option value="10">10</option>
                        <option value="11">11</option>
                        <option value="12">12</option>

                    </select>
                    <select id="exYear" name="exYear">

                        <option value="2024">2024</option>
                        <option value="2025">2025</option>
                        <option value="2026">2026</option>
                        <option value="2027">2027</option>
                        <option value="2028">2028</option>

                    </select>
                </div>
        </fieldset>
    
        <br>

        <input type="submit">
        <button>Help</button>

        <p id="demo"></p>
        <script src="../js/assessment1.js">
        </script>

Javascript

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> function validateForm()
{
var exMonth=document.getElementById("exMonth");
var exYear=document.getElementById("exYear");
var date = new Date ();
var month = date.getMonth();
var year = date.getFullYear();
if (year> exYear || (year === exYear && month >= exMonth))
{
alert("Your expirey date has passed");
return false;
}
alert("Thanks");
return true;
}
</code>
<code> function validateForm() { var exMonth=document.getElementById("exMonth"); var exYear=document.getElementById("exYear"); var date = new Date (); var month = date.getMonth(); var year = date.getFullYear(); if (year> exYear || (year === exYear && month >= exMonth)) { alert("Your expirey date has passed"); return false; } alert("Thanks"); return true; } </code>
    function validateForm()
{
    var exMonth=document.getElementById("exMonth");
    var exYear=document.getElementById("exYear");
    var date = new Date ();
    var month = date.getMonth();
    var year = date.getFullYear();

    if (year> exYear || (year === exYear && month >= exMonth))
        {
            alert("Your expirey date has passed");
            return false;
        }
    alert("Thanks");
    return true;
}

I’ve honestly been troubleshooting so much I’ve lost what I’ve already tried. I’ll take any advice that can be provided 🙂

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật