0

I am working with a mysql database that takes dates as YYYY-MM-DD. I have an array named dates that contains so inputted values, I am trying to check to see if the inputted values are in the format YYYY-MM-DD and if they aren't then add them to the wrongdates array however i am completely lost on the logic that goes in the if statement to check if the format is correct

var wrongdates = [];
for(x=0;x<dates.length<x++){
    if(.........){
        wrongdates.push(dates[x].value);
    }
}
window.alert(wrongdates);
Anna
  • 23
  • 2
  • You can use a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) to see if a string matches a specific format. However, whether that is "good enough" depends on your requirements. See https://stackoverflow.com/q/15491894/218196 for details. – Felix Kling Nov 27 '19 at 20:36
  • Possible duplicate of [Regex to validate date format dd/mm/yyyy](https://stackoverflow.com/questions/15491894/regex-to-validate-date-format-dd-mm-yyyy) – diogenesgg Nov 27 '19 at 20:48

2 Answers2

2

You can use regex to ensure the structure.

const dates = ['2019-99-99', 'bla-bal-bla', '2019-11-20'];

const wrongdates = dates.filter(date => !/\d{4}\-[01]\d\-[0-3]\d/.test(date));

console.log(wrongdates);
felixmosh
  • 20,379
  • 4
  • 36
  • 56
1

I would use a regular expression. This one should work for you:

\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])

This checks for:

\d{4}
  - four digits  
-
  - followed by a hyphen  
(?:0[1-9]|1[0-2])
  - one of:
    a. a zero followed by any number 1 to 9 (months 01 to 09)
    b. a one followed by any number 0 to 2 (months 10 to 12)
-
  - followed by a hyphen
(?:0[1-9]|[12][0-9]|3[01])
  - using the same logic as above, any number from 01 to 31.

These more complex groups will mean that dates outside of the acceptable range (things like 2019-49-40) will also fail.

If you want to accept dates like 2019-1-4 (which, given you're working with SQL, is probably a bad plan, but just in case), simply put a ? after the zeroes in the day and month sections (\d{4}-(?:0?[1-9]|1?[0-2])-(?:0?[1-9]|[12]?[0-9]|3?[01])), to allow them to match 0 or 1 times (make them optional).

Hopefully you notice that if your only data source is a plain-old string, it is impossible to verify whether someone enters a date incorrectly (ie, Feb 9th, 2019 → 2019-02-09 and 2019-09-02 will both be "correct").


To do this in Javascript, you can use the RegExp.prototype.test method. You can also leverage Array.prototype.filter if you're making a new array (and Array.prototype.map if you're working with DOM elements).

const dates = [...document.querySelectorAll('input')];

const wrongdates = dates.map(el => el.value).filter(date => !/\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])/.test(date));

console.log(wrongdates)
<input type="text" value="2019-01-31">
<input type="text" value="2019-15-52">
<input type="text" value="2005-09-17">
<input type="text" value="2001-06-13">
<input type="text" value="2001-06-13">
<input type="text" value="2001-06-13">
<input type="text" value="06-28-13">
<input type="text" value="Jan-12-2019">

You need the ! because RegExp.test returns true when it matches, and you're trying to make a list of incorrect dates.

matthew-e-brown
  • 2,039
  • 1
  • 5
  • 21