Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Datepicker only accept text that matches format #2886

Closed
stephendparker opened this issue Oct 25, 2014 · 4 comments
Closed

Datepicker only accept text that matches format #2886

stephendparker opened this issue Oct 25, 2014 · 4 comments

Comments

@stephendparker
Copy link

If someone is using a datepicker with popup, like here:

http://angular-ui.github.io/bootstrap/#/datepicker

Is it possible to mark the field as invalid until they have formatted the date like provided? For instance, if I am typed:
10
It treats this as a valid date (october 1, 2001) . I would rather treat this as invalid, until I completed by typing of my date like:
10/20/2014

@roadsunknown
Copy link

Agreed. Several inputs are not valid, but all are treated as valid. Model should be set to invalid if date 1) is not valid and 2) does not match format requested.

Example of invalid dates that are not set invalid with a format of MM/dd/yyyy:

10
10/10
10/10/10

@TyBoss
Copy link

TyBoss commented Dec 8, 2014

Hello,

I think I am encountering a similar issue. The date from the datepickerpopup marks a date valid even if the date does not match the format.

I found this line inside of the datepickerPopup parseDate function:

var date = dateParser.parse(viewValue, dateFormat) || new Date(viewValue);
if (isNaN(date)) {
ngModel.$setValidity('date', false);
return undefined;
} else {
ngModel.$setValidity('date', true);
return date;
}

The call to the dateParser service returns undefined if the date fails validation based on some regex rules defined in it.

The other side of the or expression attempts to create new date object with the value passed in.

If the new Date logic was removed from the or expression and the date is set as undefined, then the if check for isNaN(date) would pass and the model will be set to invalid.

Not sure if that would be an acceptable solution, but the date would be undefined and the model would be set to invalid in this case.

@jmayday
Copy link

jmayday commented Feb 13, 2015

Ahh. Seems like I haven't noticed this issue and also created a similar PR for this. #3294

@wesleycho
Copy link
Contributor

This should now be fixed in master.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants