如何验证角度的日期格式

时间:2022-12-09 19:44:07

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 ?

嗨,我只是想知道问题,例如,如果我有角度UI日期Piker日期格式yyyy-MM-dd。如果用户输入日期通过键入,他们输入错误的格式,例如yyyy-dd-mm应用程序认为它是有效的日期并保存为YYYY-MM-dd。我怎样才能验证格式是否正确?

<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>

validator

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

3 个解决方案

#1


Momentjs library might be useful for you.

Momentjs库可能对您有用。

moment("2010 13",           "YYYY MM").isValid();     // false (not a real month)
moment("2010 11 31",        "YYYY MM DD").isValid();  // false (not a real day)
moment("2010 2 29",         "YYYY MM DD").isValid();  // false (not a leap year)
moment("2010 notamonth 29", "YYYY MMM DD").isValid(); // false (not a real month name)

#2


Use native static Date.parse() method or take a look at momentjs library, which has great date abstraction over it.

使用本机静态Date.parse()方法或者看看momentjs库,它对它有很好的日期抽象。

Also you can use regular expression to check your date, e.g.

您也可以使用正则表达式来检查日期,例如

/\d{4}-\d{2}-\d{2}/.test($scope.model.date)

but it should come in the pair with Date.parse, couse 9999-99-99 will pass through such simple validator.

但它应该与Date.parse配对,couse 9999-99-99将通过这样简单的验证器。

Anyway, here some regexps for date validation to look at.

无论如何,这里有一些用于日期验证的regexp来查看。

#3


I update your FormValidator function to use native JavaScript

我更新您的FormValidator函数以使用本机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

#1


Momentjs library might be useful for you.

Momentjs库可能对您有用。

moment("2010 13",           "YYYY MM").isValid();     // false (not a real month)
moment("2010 11 31",        "YYYY MM DD").isValid();  // false (not a real day)
moment("2010 2 29",         "YYYY MM DD").isValid();  // false (not a leap year)
moment("2010 notamonth 29", "YYYY MMM DD").isValid(); // false (not a real month name)

#2


Use native static Date.parse() method or take a look at momentjs library, which has great date abstraction over it.

使用本机静态Date.parse()方法或者看看momentjs库,它对它有很好的日期抽象。

Also you can use regular expression to check your date, e.g.

您也可以使用正则表达式来检查日期,例如

/\d{4}-\d{2}-\d{2}/.test($scope.model.date)

but it should come in the pair with Date.parse, couse 9999-99-99 will pass through such simple validator.

但它应该与Date.parse配对,couse 9999-99-99将通过这样简单的验证器。

Anyway, here some regexps for date validation to look at.

无论如何,这里有一些用于日期验证的regexp来查看。

#3


I update your FormValidator function to use native JavaScript

我更新您的FormValidator函数以使用本机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