4

I use a function that check if entered value is a valid text for specific purpose in my application.

valid value is a string where it's not valid date or number neither true or false.

checkText(str) {
    return isNaN(str) && isNaN(Date.parse(str)) && ['true', 'false'].indexOf(str) == -1;
} 

It works properly, but i faced an issue with this string: "New Item 3".

Date.parse("New Item 3") returns a number, but why!!? also, if you changed 3 into any number less than 13 it will return number!

Anyone here can explain to me what happens?

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Nimer Awad
  • 3,967
  • 3
  • 17
  • 31
  • 1
    It will return a number no matter how many strings are preceeding it. E.g. `Date.parse("New Item Blah Foo 3")` and `Date.parse("N 3")` will also be correctly parsed. I guess the last number in the string is interpreted as a month, that's why it returns `NaN` when it's bigger than 12. I tested it on the newest versions of Brave, Chromium, and Chrome (all of them use V8). – Tomasz Kasperczyk Oct 29 '19 at 15:38
  • 2
    Non-standard date strings are handled platform-dependently. Perhaps whatever environment you tried this in somehow figures out this is a non-invalid string and parses it using some bizarre rules. It doesn't work in Firefox, for example. – VLAZ Oct 29 '19 at 15:38
  • 1
    `Date.parse("New Item 3")` returns `NaN` in FF70. `typeof NaN` is "number", though. – Teemu Oct 29 '19 at 15:38
  • 2
    Interestingly, in Firefox and IE11 `Date.parse("New Item 3")` returns `NaN`, in Chrome/Chromium it returns `983401200000` – Constantin Groß Oct 29 '19 at 15:38
  • 2
    From MDN: "It is not recommended to use Date.parse as until ES5, parsing of strings was entirely implementation dependent. There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated)." – Scott Marcus Oct 29 '19 at 15:39
  • 1
    `Date.parse` behaves inconsistently across browsers, as the string parsing is implementation dependent. You shouldn't use it at all. – 31piy Oct 29 '19 at 15:39

2 Answers2

6

Lesson learned: Date.parse is not a date validator.

Even MDN says:

It is not recommended to use Date.parse as until ES5, parsing of strings was entirely implementation dependent. There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated).

And further down

The ECMAScript specification states: If the String does not conform to the standard format the function may fall back to any implementation–specific heuristics or implementation–specific parsing algorithm. Unrecognizable strings or dates containing illegal element values in ISO formatted strings shall cause Date.parse() to return NaN.

However, invalid values in date strings not recognized as simplified ISO format as defined by ECMA-262 may or may not result in NaN, depending on the browser and values provided

Community
  • 1
  • 1
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 4
    "*Lesson learned: `Date.parse` is not a date validator.*" indeed, it's a date *parser*. Furthermore, it's more like `letMyCurrentEnvironmentTryToParseThisStringUsingSomeAdditionalNonStandardAndLegacyRulesThatAreProbablyNotWellDocumentedIfAtAll` Although I guess that name might be slightly too long, even if descriptive. – VLAZ Oct 29 '19 at 15:41
4

In fact the problem here is coming from Date.parse() method, if you check:

Date.parse("New Item 3");

It will return:

983401200000

console.log(Date.parse("New Item 3"));

So the fact here is that Date.parse() will behave according the browser specifications and may or not return a Number. It depends on the browser.

And you can see from the Date.parse() MDN reference that:

The ECMAScript specification states: If the String does not conform to the standard format the function may fall back to any implementation–specific heuristics or implementation–specific parsing algorithm. Unrecognizable strings or dates containing illegal element values in ISO formatted strings shall cause Date.parse() to return NaN.

However, invalid values in date strings not recognized as simplified ISO format as defined by ECMA-262 may or may not result in NaN, depending on the browser and values provided.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78