I have three inputs which are for the Day, Month and Year.
<div id="birthday">
<div>
<label for="day">Day</label>
<input type="number" id="day" placeholder="Day" name="day" ref="day" />
</div>
<div>
<label for="month">Month</label>
<input type="number" id="month" placeholder="Month" name="month" ref="month" />
</div>
<div>
<label for="year">Year</label>
<input type="number" id="year" placeholder="Year" name="year" ref="year" />
</div>
<span class="clear_both"></span>
</div>
I want to validate date by the following:
Year
- Year should have 4 characters (i.e., YYYY)
- Year should be between 1900 to present year.
Month
- Month should be between 1 to 12
Day
- If the year is a leap year and month is 2 (february) then Day should be between 1 to 29
- If the year is not a leap year, then the Day should be between 1 to 31 or 1 to 30 depending on the month
I can only check the month and year:
let day = this.refs.day.value
let month = this.refs.month.value
let year = this.refs.year.value
let errors = []
if (!((year.length == 4) && (year > 1900 && year < 2016))) {
errors.push("year");
}
if (!(month > 0 && month < 13)) {
errors.push("month");
}
How can I make this work in javascript? Could you please help me. Thank you.