AngularJS date validation (formats)

751 views Asked by At

I could use some help. I am having input field with type="text" and ng-required="true". I am using datepicker (Angular UI Bootstrap).

If my input is empty, someForm.someInput.$error returns "required":true, which is okay. If my input is filled with "someText", someForm.someInput.$error returns "date":true, which is odd to me.

  1. How is this triggered, is this done by directive datepicker? I don't have type="date" or something.*

  2. How could I achieve that date in format "dd.MM.yyyy" would be valid?

If I enter 01.01.2015 or 01.13.2015 - this seems to be valid (no "date":true) but apparently it isn't. I guess current valid format is "yyyy.MM.dd".

Note: I am also using angular locale file, if this is important.

2

There are 2 answers

0
be-codified On BEST ANSWER

I found out that datepicker is using two values. One for model, one for input. This works if you enter date by selecting it from popup, although not working if typing it in input field.

I have solved this issue by putting readonly attribute to input field.

0
Sagar Ranglani On

In my case (RoR and AngularJS), I was saving the data into the server the next time when you get the data from the server, the datepicker has {date: true} validation error.

What I did to fix this? Instead of sending iso8601 format string or UTC format string, I tried returning the milliseconds format

Following example in ruby will clarify what removed the date validation on angular ui bootstrap datetimePicker.

my_object.my_timestamp # DID NOT WORK :-(
my_object.my_timestamp.iso8601 # DID NOT WORK :-(

my_object.my_timestamp.to_f * 1000 # WORKED :-)

Hope this helps!