-1

How to create such javascript that check date validation for format YYYYMM. and gives an alert when the date is entered in any other format other than YYYYMM.

Code Lღver
  • 15,434
  • 16
  • 54
  • 74
  • 4
    Hi there! [so] is not a place where people will just write code for you. You'll have to prove that you have attempted to solve this yourself or at least show some research effort... Where exactly are you having problems? – Lix Apr 02 '13 at 10:43
  • consider using regular expressions – Muhammad Adeel Zahid Apr 02 '13 at 10:44

2 Answers2

1

using moment.js:

 return moment(yourDate, 'YYYYMM').isValid()

See http://momentjs.com/docs/#/parsing/is-valid/ for more examples.

georg
  • 204,715
  • 48
  • 286
  • 369
0

You can use a regular expression to accomplish this (which can be quite complex when checking for several date formats). Have a look at this SO link for more information.

A useful library for dealing with dates is XDate (which does not check for a specific date format!).

You can easily check if a date is valid using the .valid() method:

var mydate = new XDate('2012-06-08', true);
if (mydate.valid()) {
    console.log('date is valid!');
}
Community
  • 1
  • 1
akluth
  • 8,085
  • 4
  • 37
  • 42
  • This looks like a useful library - but I don't see from your example any thing regarding validating a certain date **format**... – Lix Apr 02 '13 at 10:46
  • It also looks like some serious customization is needed for this plugin to operate as the OP requested. A good library - yes... but I don't think that this would help the OP very much. – Lix Apr 02 '13 at 10:49
  • Snap, indeed it does not check for a certain date format...didn't think about that, thanks for mentioning! – akluth Apr 02 '13 at 10:51
  • no problem :) ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ – Lix Apr 02 '13 at 10:51
  • Updated my answer, I let the XDate example there, I think it is quite useful when dealing with dates. – akluth Apr 02 '13 at 10:57