I'm having trouble with my web programming class assignment, I'm supposed to use a regular expression in javascript to validate a date input, mine is coming back in the ISO8601 format.
This is the expression im using
/^(\d{4})-(\d{2})-(\d{2})$/
So far it's still returning as an invalid date even when the date is correct. here's the regular expression as it looks inside notepad++.
The value for the date input is coming back as "2020-09-26" according to the console
I'm using
var order_date = "2020-09-26"
var regDate = new RegExp('^(\d{4})-(\d{2})-(\d{2})$');
if (!regDate.test(order_date)){
valid = false;
document.getElementById("err5").innerHTML = "** Not a valid date";
}
to check the value of the input field and if it isn't valid, to set the valid variable to false and add an error message to an error span in the HTML. Here's a screenshot of the code itself in my editor as well.
Before I tried using a regular expression, the statement was
if(order_date === "" || order_date.length < 8 || order_date.length > 10)){
valid = false;
document.getElementById("err5").innerHTML = "** Not a valid date";
}
and it technically worked, it could accept an ISO8601 format date and tell if it fit or not and return valid, but obviously this check can accept an invalid date, so we were told to use a regular expression to improve the check.
Just need some help as to why this wouldn't be working. From what I can tell, there's nothing wrong with the regex itself, so maybe it's the rest of my javascript or some other issue? any suggestions are helpful