我已限制用戶將來使用 html 5 日期選擇器在輸入中輸入日期。但用戶可以輸入未來的日期。如果用戶不選擇從日期選擇器日歷中選擇日期值,有沒有辦法阻止用戶輸入未來的日期?這是我的代碼:HTML <div class="col-sm-7"> <input type="date" class="form-control" [max]="maxDate" pattern="^(19[5-9][0-9]|20[0-4][0-9]|2050)[-/](0?[1-9]|1[0-2])[-/](0?[1-9]|[12][0-9]|3[01])$" name="Datebillabuse" [(ngModel)]="Datebillabuse" #date="ngModel" [ngClass]="{ 'is-invalid': f.submitted && date.invalid }" required /> <div *ngIf="f.submitted && date.invalid" class="invalid-feedback"> <div *ngIf="date.errors.required">Date is required</div> <div *ngIf="date.errors.pattern">Please enter a valid date</div> <div *ngIf='date.errors.max'>Date must not be in future</div> </div> </div>打字稿 setTodayDate() { const dtToday = new Date(); let month = String(dtToday.getMonth() + 1); let day = String(dtToday.getDate()); let year = dtToday.getFullYear(); if (parseInt(month, 10) < 10) { month = '0' + month.toString(); } if (parseInt(day, 10) < 10) { day = '0' + day.toString(); } this.maxDate = `${year}-${month}-${day}`; }我想到的一種解決方案是檢查日期值是否大于今天并顯示錯誤。有沒有更好的方法可以在 Angular 中解決它?
如何阻止用戶在 angular 的日期類型輸入中輸入未來日期
喵喔喔
2021-10-21 14:06:56