Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MomentJS - Intended for input validation?

Is MomentJS intended for user input parsing?

I've got moderately close with the easy cases, having it accept dates in the DDMMYYYY order, and it handles some variation.

It doesn't handle invalid dates particularly well when specifying the format - Including having day values too high, or switching year values between 2 and 4 digit.

Examples of year interpretation:

var date1 = moment('30082012', 'DDMMYYYY');
var date2 = moment('30082012', 'DDMMYY'); // Gives wrong year - 2020
var date3 = moment('300812', 'DDMMYYYY'); // Gives wrong year - 1900
var date4 = moment('300812', 'DDMMYY');

Examples of what would hopefully be invalid dates:

var date5 = moment('08302012', 'DDMMYYYY'); // Gives Jun 08 2014
var date6 = moment('08302012', 'DDMMYY'); // Gives Jun 08 2022
var date7 = moment('083012', 'DDMMYYYY'); // Gives Jun 08 1902
var date8 = moment('083012', 'DDMMYY'); // Jun 08 2014

I have created a JS Fiddle with these examples: http://jsfiddle.net/cHRfg/2/

Is there a way to have moment accept a wider array of user input, and reject invalid dates? Or is the library not intended for this?

like image 400
Overflew Avatar asked May 23 '12 00:05

Overflew


People also ask

What is MomentJS used for?

MomentJS is a JavaScript library which helps is parsing, validating, manipulating and displaying date/time in JavaScript in a very easy way. This chapter will provide an overview of MomentJS and discusses its features in detail. Moment JS allows displaying of date as per localization and in human readable format.

Do you need MomentJS?

You do not need Moment. js if you support newer browsers and if you show dates or datetimes only in your user's timezone. Things are not as easy as they seem, especially if you plan on manually parsing the output from the native APIs.

Is MomentJS deprecated?

moment().zone is deprecated, use moment().utcOffset instead.

How do I get time from MomentJS?

You can directly call function momentInstance. valueOf(), it will return numeric value of time similar to date. getTime() in native java script.

How do I get current year in MomentJS?

year() method is used to get or set the year of the Moment object. The year value that can be set has a range from -270,000 to 270,000. Syntax: moment().


2 Answers

var parsed = moment(myStringDate, 'DD.MM.YYYY');

for Version >= 1.7.0 use:

parsed.isValid()

for Version < 1.7.0 create your own isValid() function:

function isValid(parsed) { 
    return (parsed.format() != 'Invalid date');
}    

checkout the docs: http://momentjs.com/docs/#/parsing/is-valid/

like image 152
drdrej Avatar answered Sep 30 '22 08:09

drdrej


You can try parsing multiple formats. Updated fiddle: http://jsfiddle.net/timrwood/cHRfg/3/

var formats = ['DDMMYYYY', 'DDMMYY'];
var date1 = moment('30082012', formats);
var date4 = moment('300812', formats);

Here are the relevant docs. http://momentjs.com/docs/#/parsing/string-formats/

There is development on adding moment.fn.isValid which will allow you to do validation like in examples 5-8. It will be added in the 1.7.0 release. https://github.com/timrwood/moment/pull/306

like image 37
timrwood Avatar answered Sep 30 '22 07:09

timrwood