md-datepicker shows error when date entered manually

怎甘沉沦 提交于 2020-07-06 12:31:32

问题


I am using md-datepicker, when i put date manually it is showing invalid date but if i select date from this control then it is validated. for this i have used following code

    <md-datepicker name="FromDate" ng-model="vm.user.FromDate" ng-required="true" id="FromDate" md-is-error="login.ToDate.$invalid && login..$submitted" md-placeholder="FromDate"></md-datepicker> 
<div ng-messages="login.FromDate.$error" md-auto-hide="false" ng-show="login.FromDate.$touched ||login.FromDate.$submitted">
<div ng-message="required">Please enter FromDate.</div>                    </div>

回答1:


i found the answer. in config section along with formatDate i added parseDate function and it is working fine no error while manual entry as well as selection from control

  .config(function ($mdDateLocaleProvider) {
        $mdDateLocaleProvider.formatDate = function (date) {
            return date ? moment(date).format('DD-MM-YYYY') : '';
        };

        $mdDateLocaleProvider.parseDate = function (dateString) {
            var m = moment(dateString, 'DD-MM-YYYY', true);
            return m.isValid() ? m.toDate() : new Date(NaN);
        };
    });



回答2:


please check the format of the date that you are entering the default format is mm/dd/yyyy



来源:https://stackoverflow.com/questions/44432802/md-datepicker-shows-error-when-date-entered-manually

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!