简体   繁体   English

ngx-bootstrap datepicker 验证无效日期

[英]ngx-bootstrap datepicker validate invalid date

When an invalid date is entered, I want to save or keep the value entered so I can display it to the user.当输入无效日期时,我想保存或保留输入的值,以便我可以将其显示给用户。 How do I do this?我该怎么做呢?

Background : I'm using the ngx-bootstrap v5.1.0 datepicker and angular 7.2.15.背景:我正在使用 ngx-bootstrap v5.1.0 datepicker 和 angular 7.2.15。 When a date is invalid (ex:09/99/2020 using MM/DD/YYYY), it automatically puts the string "Invalid date" into the input box when I leave the input field.当日期无效时(例如:09/99/2020 使用 MM/DD/YYYY),当我离开输入字段时,它会自动将字符串“无效日期”放入输入框中。

This technique works but additionally, I'd like to give the user some sort of feedback about the date that they entered.这种技术有效,但另外,我想给用户一些关于他们输入日期的反馈。 Specifically, the value entered is invalid.具体来说,输入的值无效。 In this case, it would be 09/99/2020.在这种情况下,它将是 09/99/2020。

I can get the invalid date validation to fire, but I can't get the value of the date originally entered so the user knows what they entered and why it's invalid.我可以触发无效日期验证,但我无法获取最初输入的日期值,因此用户知道他们输入的内容以及无效的原因。

在此处输入图片说明

How do I get or keep the original value entered into the field?如何获取或保留输入到字段中的原始值? I've tried an onChange event and valueChanges but the ngx-datepicker seems to override this so it doesn't fire until you leave the field.我已经尝试过 onChange 事件和valueChangesngx-datepicker似乎覆盖了它,因此在您离开该领域之前它不会触发。

ngx-bootstrap-datepicker has a custom Output event bsValueChange ngx-bootstrap-datepicker 有一个自定义输出事件bsValueChange

Add the output to your HTML input element:将输出添加到您的 HTML input元素:

  <input type="text"
        class="form-control"
        [minDate]="minDate"
        [maxDate]="maxDate"
        #dp="bsDatepicker"
        (bsValueChange)="onDateChange($event)"
        bsDatepicker [(bsValue)]="myDateValue">

and then handle this in your component ts:然后在你的组件 ts 中处理这个:

 myDateValue: Date;
  previousDate: Date;

  ngOnInit() {
    this.myDateValue = new Date();
  }

  onDateChange(newDate: Date) {
    this.previousDate = new Date(newDate);

Example stackblitz: https://stackblitz.com/edit/ngx-bootstrap-datepicker-taconz示例堆栈闪电战: https ://stackblitz.com/edit/ngx-bootstrap-datepicker-taconz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM