如何在 angular 模板驱动方法中验证开始和结束日期?
How to validate start and end date in angular template driven approach?
我的表单中输入了 2 个日期,如下所示:
<label class="control-label">Start Date</label>
<input type="date" name="startDate" placeholder="Start Date" class="form-control" ngModel required #startDate="ngModel">
<span *ngIf="!startDate.valid && startDate.touched" class="error-msg">
<span *ngIf="startDate.errors.required">Start Date is required</span>
</span>
<br/>
<label class="control-label">End Date</label>
<input type="date" name="endDate" placeholder="End Date" class="form-control" ngModel required #endDate="ngModel" >
<span *ngIf="!endDate.valid && endDate.touched" class="error-msg">
<span *ngIf="endDate.errors.required">End Date is required</span>
</span>
而且我想允许用户 select 日期仅从开始日期开始。我尝试使用 min
属性但没有工作
您可以使用 min
属性
<label class="control-label">End Date</label>
<input type="date" name="endDate" placeholder="End Date" class="form-control" ngModel required #endDate="ngModel" min="{{your_form_name.value.startDate}}">
我的表单中输入了 2 个日期,如下所示:
<label class="control-label">Start Date</label>
<input type="date" name="startDate" placeholder="Start Date" class="form-control" ngModel required #startDate="ngModel">
<span *ngIf="!startDate.valid && startDate.touched" class="error-msg">
<span *ngIf="startDate.errors.required">Start Date is required</span>
</span>
<br/>
<label class="control-label">End Date</label>
<input type="date" name="endDate" placeholder="End Date" class="form-control" ngModel required #endDate="ngModel" >
<span *ngIf="!endDate.valid && endDate.touched" class="error-msg">
<span *ngIf="endDate.errors.required">End Date is required</span>
</span>
而且我想允许用户 select 日期仅从开始日期开始。我尝试使用 min
属性但没有工作
您可以使用 min
属性
<label class="control-label">End Date</label>
<input type="date" name="endDate" placeholder="End Date" class="form-control" ngModel required #endDate="ngModel" min="{{your_form_name.value.startDate}}">