1

I have read lots of website for date checking. However, some of those are useless because I want to set the date format to dd/MM/yyyy.

var result = new Date(input);
if (result == 'Invalid Date') {
    return false;

For the code above,

  • it is still valid if i enter '60/09/02016'.
  • it return MM/dd/yyyy
king yau
  • 500
  • 1
  • 9
  • 28
  • I tried new Date(''60/09/02016'') it is also invalid – Beginner Sep 27 '16 at 07:09
  • You can simply use moment.js. It will allow you check valid date in any format you want – R.K.Saini Sep 27 '16 at 07:15
  • You need to call result.toString() in your if condition. Then it will return "Invalid Date" if date is not valid – R.K.Saini Sep 27 '16 at 07:24
  • @user3273700 it return "2020-12-05T16:00:00.000Z" if i enter Date(''60/09/02016'') – king yau Sep 27 '16 at 07:44
  • @user3273700 i cannot use moment.js because the client only allows me to use jQuery for javascript library :( Therefor, i have no idea how to do this.(I know how to use moment.js) – king yau Sep 27 '16 at 07:46
  • @kingyau what's the client's objection to momentJS? If you explain it will save you time to solve a problem it's hard to see the problem, especially if jQuery is already permitted. Otherwise you have to re-invent the wheel...and it's a very complicated wheel. – ADyson Sep 27 '16 at 08:08
  • @ADyson—it's a very simple wheel. Parsing and validating the OP format is 3 lines of code. – RobG Sep 28 '16 at 02:55
  • @RobG even ensuring that the date actually exists (e.g. extra days in leap years and all that?) And I was referring to date parsing in general really. I'm sure Moment wouldn't be so popular if anyone could knock it together in 3 lines – ADyson Sep 28 '16 at 07:29
  • @RobG but if you can do all that in 3 lines then all power to you, I'll be duly impressed – ADyson Sep 28 '16 at 07:46
  • 1
    @ADyson—for sure, a general parser is a bit more work (and a good exercise). There are plenty of small libraries that do parsing very well, so no point in reinventing that wheel. But for a single format, it's quite easy. In modern browsers, the OP format can be validated with `new Date(s.split(/\D/).reverse().join('-')) != 'Invalid Date'`. But I wouldn't suggest actually doing that (since it relies on unreliable parsing and possibly confusing implicit conversion of Date object to string primitive). ;-) – RobG Sep 28 '16 at 23:23

3 Answers3

1

If you're able to use the moment.js (http://www.momentjs.com) library in your code, then it's very simple:

var input = '60/09/2016';
var dt = moment(input,'DD/MM/YYYY');
alert(dt.isValid()); //returns true or false depending on whether the date is valid and in the correct format
ADyson
  • 57,178
  • 14
  • 51
  • 63
0

You can use Moment to validate date. Follow the below code may be its is useful for you !

     var moment = require('moment')
     var m = moment('2015-11-32', 'YYYY-MM-DD');
     console.log(m.isValid()) // false

For Browser you can use cdn or download js from moment site

   <script src="moment.js"></script>
   <script>
     var m = moment('2015-11-32', 'YYYY-MM-DD');
     console.log(m.isValid()) // false
  </script>

Refer : http://momentjs.com

JS_Link : http://momentjs.com/downloads/moment.js

download moment.js and refer in your project.

Selva Kumar
  • 839
  • 1
  • 10
  • 15
  • "require" will only work on nodeJS, not in the browser. OP didn't specify either way. And also the date format isn't the one asked for by the OP. Otherwise the example is ok. – ADyson Sep 27 '16 at 07:20
  • You can use the same in browser, Follow the updated answer – Selva Kumar Sep 27 '16 at 07:36
  • ok that's more comprehensive - thanks – ADyson Sep 27 '16 at 07:37
  • If there is any struggle it working or not let me know its help ful for you – Selva Kumar Sep 27 '16 at 07:52
  • no it's fine, I know how to use it (see my answer below) just wanted to clarify for OP the difference between node and browser references to the library – ADyson Sep 27 '16 at 07:57
  • I did't get you. Can you explain in detail .What you need to know – Selva Kumar Sep 27 '16 at 08:00
  • _I_ don't need to know anything. I wanted you to clarify the question for the _OP's_ benefit, which you have done, and I have thanked you for already. I'm not really sure why we are still talking about this :-) – ADyson Sep 27 '16 at 08:07
  • What i am thinking na you expect something to know. So that only i am asking .Sorry for that . For your clarification that is **OP** difference between that two is not so much difference both are giving same response the place of compile is only differ. – Selva Kumar Sep 27 '16 at 08:14
  • Answers should explain the OPs issue and why your answer fixes it. Answers should not depend on libraries that are not tagged or asked for in the OP. – RobG Sep 28 '16 at 00:36
0

Use datepicker then and restrict the user to input any characters on it.

Ki v2
  • 46
  • 9