I am very new to Javascript and am trying to ensure the date entered in a textbox (i haven't used number as im playing) is not less than todays date. I think I have over complicated this, please can you help me with the logic.
I start by reading user date and split user input into dd/mm/yyyy
//date regex ensures day and month != 00 && is a valid date
var dateformat = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])
[\/\-]\d{4}$/;;
//store user date
var readDate = document.getElementById("myDate").value;
//if date is valid
if(dateformat.test(readDate))
{
var day = splitDate[0];
var month = splitDate[1];
var year = splitDate[2];
var dateToday = new Date()
var dd = dateToday.getDate()
var mm = dateToday.getMonth() + 1
var yyyy = dateToday.getFullYear()
console.log('date today is: ' + dd + '/' + mm + '/' + yyyy);
if(day > dd)
{
if(month >= mm)
{
if(year >= yyyy)
{
console.log('it works - continue')
return true;
}
else
{
console.log('old year')
return false;
}
}
else
{
console.log('old month')
return false;
}
}
else
{
console.log('old day')
return false;
}
}
else
{
console.log('invalid date')
}
I know this is very very dirty but I am so confused with the logic and before experts start using lambdas etc that i am not use too I just want to find out why this will not work? and how to get it to work? surely it is
I wrote this out to follow but it still does not help me
Date is valid when: User day > current day
User month > = current month
User year > = current year
Today’s date = 21/11/2017
So if user enters:
21/11/2017 not valid
02/03/2017 not valid
22/12/2017 valid
but its not working
why oh why?
have i over complicated this?
it would be wonderful to know why it does not work in a very simple and easy way
thank you
p.s. if there is an easier way but keeping my dateformat, readDate and splitDate intact please can you help me find it? thanks