如何验证有角度的日期格式 [英] how to validate date format in angular

查看:102
本文介绍了如何验证有角度的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道问题,例如,如果我有角度UI日期piker日期格式yyyy-MM-dd。如果用户输入日期,并输入错误的格式,例如yyyy-dd-mm应用程序思考它是有效的日期并保存为YYYY-MM-dd。如何验证格式是正确的?

Hi just I was wondering about problem for example if I have angular UI date piker date format yyyy-MM-dd .what if the users enter date by typing and they enter wrong format for example yyyy-dd-mm the app think it is valid date and save it as YYYY-MM-dd. just how can I validate format it is correct ?

<div class="panel panel-default">
    <div class="panel-heading">Date </div>
    <div class="panel-body">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label class="control-label"><i class="fa fa-calendar"></i><i class="icon-required"></i>Date [YYYY-MM-DD]</label>
                    <div class="input-group">
                        <input type="text" class="form-control" datepicker-popup="yyyy-MM-dd" data-ng-model="model.date" is-open="isDatePickerOpen" close-text="Close" />
                        <span class="input-group-btn">
                            <button type="button" class="btn btn-default" data-ng-click="openDatePicker($event)"><i class="glyphicon glyphicon-calendar"></i></button>
                        </span>
                    </div>
                    <div class="validation-warning" data-ng-show="displayModel.showDateValidator"><i class="icon-alert"></i>Required</div>
                </div>
            </div>
        </div>
    </div>
</div>

验证器

var formValidator = function ($scope) {
    var isDateValid = function () {
        return $scope.model != null && $scope.model.date != null && $scope.model.date !== '';
    };

    return {
        valid: function () {
            var isValid = true;
            if (!isDateValid()) {
                isValid = false;
            }

            return isValid;
        },
        addWatches: function () {
            $scope.$watch('model.date', function () {
                $scope.displayModel.showDateValidator = !isDateValid();
            });

        }
    };

};//ActivitiesFormValidator


推荐答案

p>我更新您的FormValidator函数以使用本机JavaScript

I update your FormValidator function to use native JavaScript

var formValidator = function ($scope) {
    var isDateValid = function () {
        //return $scope.model != null && $scope.model.date != null && $scope.model.date !== '';
        var dateTime = $scope.model.date;
        if (dateTime === null) return false;
        var day = dateTime.getDate();
        var month = dateTime.getMonth() + 1;
        var year = dateTime.getFullYear();
        var composedDate = new Date(year, month, day);
        return composedDate.getDate() === day &&
                 composedDate.getMonth() === month &&
                 composedDate.getFullYear() === year;

    };

    return {
        valid: function () {
            var isValid = true;
            if (!isDateValid()) {
                isValid = false;
            }

            return isValid;
        },
        addWatches: function () {
            $scope.$watch('model.date', function () {
                $scope.displayModel.showDateValidator = !isDateValid();
            });

        }
    };

};//ActivitiesFormValidator

这篇关于如何验证有角度的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆