我想验证三个字段dob,允许用户仅在用户年满18岁时注册

时间:2017-05-04 15:45:38

标签: angularjs

如果他超过18岁,我想要允许用户。这是我的控制器和HTML代码。

控制器:

$scope.days = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'];
$scope.months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
$scope.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
        $scope.years = [];
        for (var i = 1960; i < 2005; i++) {

            $scope.years.push(i);
        }

的index.html

<div class="form-group">
    <label class="control-label" for="selectDOB">DATE OF BIRTH*</label>

                <div class="col-xs-6 col-sm-5 col-md-5 date-day dateDay">
                    <select class="form-control" ng-model="dobDate" id="selectDOB" name="dobDate" ng-options="selectedday for selectedday in days" ng-change="validDate()" required>
                        <option value="">DD</option>
                    </select>
                </div>
                <div class="col-xs-6 col-sm-7 col-md-7 date-month">
                    <select class="form-control" name="dobMonth" ng-model="dobMonth" ng-change="validDate()" required month-options>
                    </select>
                </div>
            </div>

        </div>
        <div class="col-xs-6 col-sm-5 col-md-5 date-year dateYear">
            <select class="form-control" ng-model="dobYear" name="dobYear" ng-options="selectedyear for selectedyear in years | orderBy: orderBy:'value.selectedyear' " ng-change="validDate()" required>
                <option value="">YYYY</option>
            </select>
        </div>
    </div>

1 个答案:

答案 0 :(得分:1)

获取当前年份和选定年份。然后检查差异是否大于或等于18

$scope.validDate() {

    var date = new Date();
    var cYear = date.getYear()

    var diff = cYear - parseInt($scope.dobYear);
    if (diff < 18) {
        console.log("error")
    }
}