-1

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. enter image description here

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

Poul Bak
  • 10,450
  • 5
  • 32
  • 57
  • 1
    Does this answer your question? [regexp Parsing ISO-8601](https://stackoverflow.com/questions/8269349/regexp-parsing-iso-8601) – Christian Baumann Sep 26 '20 at 17:06
  • 1
    Please complete the code in your question with the `regDate` assignment and the example value assigned to `order_Date` so that we can run and reproduce the issue you describe. Not sure why you talk about notepad++ when you try to make it work in JavaScript. That makes your question confusing. Please focus on one of the two. – trincot Sep 26 '20 at 17:07
  • @trincot i added the regDate and order_date assignments, the order_date is just an example value, since it actually pulls the value from the html form I'm using, but it matches the value I'm pulling from the form – evanwilliams Sep 26 '20 at 17:37
  • @ChristianBaumann doesn't look like it, seems his issue was with checking the time at the end of the date, but the value I'm trying to validate doesn't have a time. It seems like the regex is fine, from what I've been reading, so it may be an issue with the js that im missing? – evanwilliams Sep 26 '20 at 18:50
  • The problem is `new RegExp('/^(\d{4})-(\d{2})-(\d{2})$/');`. It needs to be either `regDate = new RegExp('^(\\d{4})-(\\d{2})-(\\d{2})$');` (passing a string literal to `new RegExp`) or - better - just `regDate = /^(\d{4})-(\d{2})-(\d{2})$/;` (a regex literal). – Bergi Sep 26 '20 at 19:19
  • @Bergi it wasn't working as a literal, and it wasn't working in your suggested form either. It seems regular expressions just aren't working. I don't know what the issue is. – evanwilliams Sep 27 '20 at 03:04

1 Answers1

0

2020-09-26 format should be validated with /^\d{4}-\d{2}-\d{2}$/ Make sure to trim your date first.

const validateDate = (dateString) => {
  const regex = /^(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})$/;
  const result = regex.exec(dateString || '2020-09-26');

  if (!result) {
    return false;
  }

  const { y, m, d } = result.groups;
  console.log(y, m, d);

  if (false /* write your days per months checks here */) {
    return false;
  }

  return true;
};
Angel Zlatanov
  • 288
  • 1
  • 5
  • how is `(\d{4})-(\d{2})-(\d{2})` different than `^\d{4}-\d{2}-\d{2}$` ? – Scott Weaver Sep 26 '20 at 17:06
  • Maybe it's the way you use it, I have updated my answer, please check it, – Angel Zlatanov Sep 26 '20 at 17:11
  • it's basically the same regex as the OP is using (which works just fine in Notepad++) - I don't think we have enough info in the question. – Scott Weaver Sep 26 '20 at 17:21
  • @ScottWeaver what other information could help? This is my first question on the site, and I'm asking because I can't seem to find the answer anywhere else. – evanwilliams Sep 26 '20 at 17:39
  • @ScottWeaver I think the bit that is causing the issue is "but obviously this check can accept an invalid date" So, whilst the regular expressions shown test for nnnn-nn-nn strings - there's nothing that's checks that it is a valid date, as opposed to, say, "0000-11-99" – ATD Sep 26 '20 at 17:55
  • maybe so. gotta be just a typo here. the basic regex works fine. (although ATD is correct it could match non dates also) – Scott Weaver Sep 26 '20 at 17:56
  • Yep - the regex works just fine. A date can be checked for validity by trying to create a new Date() from it - but that will work for all values that pass the regex check as long as the mm and dd figures are valid (so, "....-12-31" is OK, but "....-12-32" is not - you get "Invalid Date"). Years can be from 0000 to 9999, so only a year-range check could confirm if that is "valid" (within the requirements laid down). So, Evan may have to use your regex as a starting point and double-check validity using javascript? – ATD Sep 26 '20 at 18:05
  • @ScottWeaver I'm aware that this could match invalid dates, the "obviously" part more refers to the previous method of validation which just checks if the string value is either not blank or within a certain character length, rather than checking if it's numbers written in a proper date format. For this assignment's purposes I just need to check if the value being passed is in the "xxxx-xx-xx" format and that it's all numbers, not necessarily whether those numbers correlate to a real date. To your point about a typo, i'll update the question with current code pasted over that isn't working – evanwilliams Sep 26 '20 at 18:08