当使用AngularJS使日期输入字段无效时,为什么没有启用具有ng-disabled属性的Submit按钮?

时间:2018-12-08 16:12:23

标签: angularjs validation form-submit

我正在使用AngularJS进行客户端表单验证。我故意将日期输入字段键入为“ hhhh”,这应该使日期字段无效,并且提交按钮被禁用。虽然这不是预期的。这是我的CDE代码段。

<div ng-app="myApp" ng-controller="myController">
    <input type="dob" id="date"  name="dob" ng-model="dob" placeholder="type your valid Date of Birth: YYYY-MM-DD" ng-pattern="^\d{4}-\d{2}-\d{2}$" class="form-control" required></input>
    <p ng-show="myForm.dob.$untouched" class="help-block">Date Of Birth is required</p>
    <p ng-show="myForm.dob.$touched && myForm.dob.$invalid" class="help-block">Please enter valid Date of Birth</p>
    <button type="submit" action="/register" class="btn btn-primary" ng-disabled="myForm.$invalid">Submit</button>
</div>

这是我的控制人:

var MultilingualApp = angular.module("myApp",[]);
MultilingualApp.controller("myController", function ($scope) {
});

我想请你指导我。

1 个答案:

答案 0 :(得分:0)

您没有使用form标记,并且输入类型必须为date而不是

<form name="myForm">
        <input type="date" id="date"  name="dob" ng-model="dob" placeholder="type your valid Date of Birth: YYYY-MM-DD" ng-pattern="^\d{4}-\d{2}-\d{2}$" class="form-control" required></input>
        <p ng-show="myForm.dob.$untouched" class="help-block">Date Of Birth is required</p>
        <p ng-show="myForm.dob.$touched && myForm.dob.$invalid" class="help-block">Please enter valid Date of Birth</p>
        <button type="submit" action="/register" class="btn btn-primary" ng-disabled="myForm.$invalid">Submit</button>
</form>

干杯!