简体   繁体   English

使用ion-datetime进行日期验证

[英]Date validation with ion-datetime

In my Ionic form I am loading ion-datetime dynamically. 以我的离子形式,我正在动态加载ion-datetime User can choose the dates one by one from 1 to ..n. 用户可以从1到..n一一选择日期。 But the second date must greater than the first one. 但是第二个日期必须大于第一个。 3rd date must be greater than both 1st and 2nd dates. 第三日期必须大于第一和第二日期。

Once fill the date if user change any date in between have to validate. 如果用户更改之间的任何日期,则必须填写一次日期。 For an example try to change 3rd date less than 4th date. 例如,尝试将第3个日期更改为小于第4个日期。

Can anyone give me a solution ? 谁能给我解决方案?

make an array that will store all date's - stored as int instead of date (will make it easier to compare) then check if index before exist, if yes then check if int in index before is smaller then check if index after exist if yes then check if it bigger. 制作一个将存储所有日期的数组-以int而不是date的形式存储(将使其更易于比较),然后检查before之前的索引是否存在,如果是,则检查inindex之前的int值是否较小,然后检查after存在的索引是否是yes然后检查是否更大。 so lets say i[1] is Your index then i[0] < i[1] < i[2] 因此,可以说i [1]是您的索引,然后i [0] <i [1] <i [2]

You should have to validate 1st date change for 2nd date like: 您必须验证第二个日期的第一个日期更改,例如:

URL: https://ionicframework.com/docs/api/components/datetime/DateTime/ 网址: https//ionicframework.com/docs/api/components/datetime/DateTime/

<ion-col>
    <ion-datetime [min]="minDate" [max]="finishMaxDate" displayFormat="DD-MM-YYYY" pickerFormat="DD MM YYYY" [(ngModel)]="eventData.startEventDate"
      placeholder="From Date" (ngModelChange)="startDateChanged()"></ion-datetime>
</ion-col>
<ion-col>
    <ion-datetime [min]="finishMinDate" [max]="finishMaxDate" displayFormat="DD-MM-YYYY" pickerFormat="DD MM YYYY" [(ngModel)]="eventData.endEventDate"
      placeholder="End Date"></ion-datetime>
</ion-col>

[min] and [max] component inputs [min]和[max]分量输入

minDate = moment().format('YYYY-MM-DDTHH:mm');
finishMinDate = moment().format('YYYY-MM-DDTHH:mm');
finishMaxDate = moment().add(10, 'years').format('YYYY-MM-DDTHH:mm');


startDateChanged() {

  if (this.eventData.startEventDate != '') {
    this.finishMinDate = moment(this.eventData.startEventDate).format('YYYY-MM-DDTHH:mm');
  }

  if (this.eventData.startEventDate != '' && this.eventData.endEventDate != '') {
    if (this.eventData.startEventDate > this.eventData.endEventDate) {
      this.eventData.endEventDate = moment(this.eventData.startEventDate).format('YYYY-MM-DDTHH:mm');
    } else {
      this.finishMinDate = moment(this.eventData.startEventDate).format('YYYY-MM-DDTHH:mm');
    }
  }
}

You can use min property and ngModel property of ion-datetime as below to fulfil your requirement. 您可以如下使用ion-datetime min属性和ngModel属性来满足您的要求。

HTML HTML

<ion-item>
  <ion-label>Date</ion-label>
  <ion-datetime displayFormat="MM/DD/YYYY" 
                [(ngModel)]="myDate" 
                (ionChange)="setDate()" 
                [min]="minDate"></ion-datetime>
</ion-item>

TS TS

export class HomePage {

  myDate; minDate;

  constructor() {

      this.minDate = '1990-12-31';
  }

  setDate() {

    this.minDate = this.myDate;
  }
}

Working Demo 工作演示

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

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