diff --git a/src/libs/ValidationUtils.js b/src/libs/ValidationUtils.js index 1bf1dce287b1..90eccdd7e94b 100644 --- a/src/libs/ValidationUtils.js +++ b/src/libs/ValidationUtils.js @@ -195,16 +195,27 @@ function isValidPaypalUsername(paypalUsername) { } /** - * Validate that "date" is between 18 and 150 years in the past + * Validate that a date meets the minimum age requirement. * * @param {String} date * @returns {Boolean} */ -function meetsAgeRequirements(date) { - const eighteenYearsAgo = moment().subtract(18, 'years'); - const oneHundredFiftyYearsAgo = moment().subtract(150, 'years'); +function meetsMinimumAgeRequirement(date) { const testDate = moment(date); - return testDate.isValid() && testDate.isBetween(oneHundredFiftyYearsAgo, eighteenYearsAgo); + const minDate = moment().subtract(CONST.DATE_BIRTH.MIN_AGE_FOR_PAYMENT, 'years'); + return testDate.isValid() && testDate.isSameOrBefore(minDate, 'day'); +} + +/** + * Validate that a date meets the maximum age requirement. + * + * @param {String} date + * @returns {Boolean} + */ +function meetsMaximumAgeRequirement(date) { + const testDate = moment(date); + const maxDate = moment().subtract(CONST.DATE_BIRTH.MAX_AGE, 'years'); + return testDate.isValid() && testDate.isSameOrAfter(maxDate, 'day'); } /** @@ -222,7 +233,7 @@ function getAgeRequirementError(date, minimumAge, maximumAge) { if (!testDate.isValid()) { return Localize.translateLocal('common.error.dateInvalid'); } - if (testDate.isBetween(longAgoDate, recentDate, undefined, [])) { + if (testDate.isBetween(longAgoDate, recentDate, undefined, '[]')) { return ''; } if (testDate.isSameOrAfter(recentDate)) { @@ -267,9 +278,9 @@ function validateIdentity(identity) { } // dob field has multiple validations/errors, we are handling it temporarily like this. - if (!isValidDate(identity.dob)) { + if (!isValidDate(identity.dob) || !meetsMaximumAgeRequirement(identity.dob)) { errors.dob = true; - } else if (!meetsAgeRequirements(identity.dob)) { + } else if (!meetsMinimumAgeRequirement(identity.dob)) { errors.dobAge = true; } @@ -432,7 +443,8 @@ function isValidTaxID(taxID) { } export { - meetsAgeRequirements, + meetsMinimumAgeRequirement, + meetsMaximumAgeRequirement, getAgeRequirementError, isValidAddress, isValidDate, diff --git a/src/pages/EnablePayments/AdditionalDetailsStep.js b/src/pages/EnablePayments/AdditionalDetailsStep.js index ca1532ba66b6..bbc9a991d14d 100644 --- a/src/pages/EnablePayments/AdditionalDetailsStep.js +++ b/src/pages/EnablePayments/AdditionalDetailsStep.js @@ -129,9 +129,9 @@ class AdditionalDetailsStep extends React.Component { errors[INPUT_IDS.LEGAL_LAST_NAME] = this.props.translate(this.errorTranslationKeys.legalLastName); } - if (!ValidationUtils.isValidPastDate(values[INPUT_IDS.DOB])) { + if (!ValidationUtils.isValidPastDate(values[INPUT_IDS.DOB]) || !ValidationUtils.meetsMaximumAgeRequirement(values[INPUT_IDS.DOB])) { ErrorUtils.addErrorMessage(errors, INPUT_IDS.DOB, this.props.translate(this.errorTranslationKeys.dob)); - } else if (!ValidationUtils.meetsAgeRequirements(values[INPUT_IDS.DOB])) { + } else if (!ValidationUtils.meetsMinimumAgeRequirement(values[INPUT_IDS.DOB])) { ErrorUtils.addErrorMessage(errors, INPUT_IDS.DOB, this.props.translate(this.errorTranslationKeys.age)); } diff --git a/src/pages/ReimbursementAccount/ACHContractStep.js b/src/pages/ReimbursementAccount/ACHContractStep.js index 414b9f44414a..18948af1c2de 100644 --- a/src/pages/ReimbursementAccount/ACHContractStep.js +++ b/src/pages/ReimbursementAccount/ACHContractStep.js @@ -66,8 +66,12 @@ class ACHContractStep extends React.Component { } }); - if (values[`beneficialOwner_${ownerKey}_dob`] && !ValidationUtils.meetsAgeRequirements(values[`beneficialOwner_${ownerKey}_dob`])) { - errors[`beneficialOwner_${ownerKey}_dob`] = this.props.translate('bankAccount.error.age'); + if (values[`beneficialOwner_${ownerKey}_dob`]) { + if (!ValidationUtils.meetsMinimumAgeRequirement(values[`beneficialOwner_${ownerKey}_dob`])) { + errors[`beneficialOwner_${ownerKey}_dob`] = this.props.translate('bankAccount.error.age'); + } else if (!ValidationUtils.meetsMaximumAgeRequirement(values[`beneficialOwner_${ownerKey}_dob`])) { + errors[`beneficialOwner_${ownerKey}_dob`] = this.props.translate('bankAccount.error.dob'); + } } if (values[`beneficialOwner_${ownerKey}_ssnLast4`] && !ValidationUtils.isValidSSNLastFour(values[`beneficialOwner_${ownerKey}_ssnLast4`])) { diff --git a/src/pages/ReimbursementAccount/RequestorStep.js b/src/pages/ReimbursementAccount/RequestorStep.js index 92108110ef49..d62cda9fa779 100644 --- a/src/pages/ReimbursementAccount/RequestorStep.js +++ b/src/pages/ReimbursementAccount/RequestorStep.js @@ -53,8 +53,12 @@ class RequestorStep extends React.Component { errors.dob = this.props.translate('bankAccount.error.dob'); } - if (values.dob && !ValidationUtils.meetsAgeRequirements(values.dob)) { - errors.dob = this.props.translate('bankAccount.error.age'); + if (values.dob) { + if (!ValidationUtils.meetsMinimumAgeRequirement(values.dob)) { + errors.dob = this.props.translate('bankAccount.error.age'); + } else if (!ValidationUtils.meetsMaximumAgeRequirement(values.dob)) { + errors.dob = this.props.translate('bankAccount.error.dob'); + } } if (!ValidationUtils.isRequiredFulfilled(values.ssnLast4) || !ValidationUtils.isValidSSNLastFour(values.ssnLast4)) {