Angular 2中的日期验证

时间:2018-05-17 03:32:51

标签: angular

这是我的方法日期验证

  //Validation Date
  validateDate(inputDate: Date, dateNow: Date) {
    if (inputDate <= dateNow) {
      return false;
    }
  }

这是我提交表单时的方法formSubmit,我在此方法中调用方法验证日期

  //When Submit Form
  formSubmits() {
    this.processValidation = true;
    let workorderss = this.workorderForm.value;

    let inputDate = new Date(workorderss.dueDate);
    let dateNow = new Date(Date.now());

    //Validation Failed
    if (this.workorderForm.invalid) {
      return;
    }
    if (this.validateDate(inputDate, dateNow) == false) {
      alert("Date Must Now!")
      return;
    }

    //Form is Valid
    this.preProcessConfigurations();

    workorderss.inspectors = { 'id': workorderss.inspectors };
    workorderss.questioners = { 'id': workorderss.questioners, 'questionerDetails': [] };
    this.workorderService.getAllWorkOrder()
      .subscribe(workorders => {
        this.workorderService.createWorkOrder(workorderss)
          .subscribe(successCode => {
            this.statuscode = successCode;
            this.workorderForm.reset();
            alert("Create WorkOrder Success");
            this.locations.back();
          },
          errorCode => alert("Code is Already Exist")
          );

      });

  }

但问题是,当我现在选择日期时,例如:“日期现在是2018年5月17日,我选择日期17可能2018年”我得到日期验证失败...如何设置验证日期当我选择现在的日期没有失败,当我在日期之前设置现在失败...在这个验证我意识到我设置日期&lt; = dateNow 但我不知道设置何时验证我选择dateNow没有失败。

1 个答案:

答案 0 :(得分:0)

<= 改为仅 <

因此,您的新验证功能将是

  validateDate(inputDate: Date, dateNow: Date) {
    if (inputDate < dateNow) {
      return false;
    }
  }