Skip to content

Commit

Permalink
update validations for all phone and fax numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
glen-aot committed Nov 22, 2024
1 parent 9045d4f commit 2d9bd63
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useFormContext } from "react-hook-form";
import { ORBC_FormTypes } from "../../../types/common";
import { CustomOutlinedInputProps } from "./CustomOutlinedInput";
import "./NumberInput.scss";
import { removeNonNumericValues } from "../../../helpers/removeNonNumericValues";

/**
* An onRouteBC customized MUI OutlineInput component
Expand All @@ -18,7 +19,7 @@ export const NumberInput = <T extends ORBC_FormTypes>(
const filterNonNumericValue = (input?: string) => {
if (!input) return "";
// only allows 0-9 inputs
return input.replace(/[^\d]/g, "");
return removeNonNumericValues(input);
};

// Everytime the user types, update the format of the users input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useFormContext } from "react-hook-form";
import "./PhoneNumberInput.scss";
import { ORBC_FormTypes } from "../../../types/common";
import { CustomOutlinedInputProps } from "./CustomOutlinedInput";
import { removeNonNumericValues } from "../../../helpers/removeNonNumericValues";

/**
* An onRouteBC customized MUI OutlineInput component
Expand Down Expand Up @@ -41,7 +42,7 @@ export const PhoneNumberInput = <T extends ORBC_FormTypes>(
export const formatPhoneNumber = (input?: string): string => {
if (!input) return "";
// only allows 0-9 inputs
const currentValue = input.replace(/[^\d]/g, "");
const currentValue = removeNonNumericValues(input);
const cvLength = currentValue.length;

// Ignore formatting if the value length is greater than a standard Canada/US phone number
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/common/helpers/removeNonNumericValues.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const removeNonNumericValues = (input: string) =>
input.replace(/[^0-9]/g, "");
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
EmailNotificationType,
} from "../../../permits/types/EmailNotificationType";
import { Optional } from "../../../../common/types/common";
import { removeNonNumericValues } from "../../../../common/helpers/removeNonNumericValues";

interface PermitResendFormData {
permitId: string;
Expand Down Expand Up @@ -227,14 +228,16 @@ export default function PermitResendDialog({
rules: {
required: false,
validate: {
validateFax: (fax?: string) =>
fax == null ||
fax === "" ||
(fax != null &&
fax !== "" &&
unformatFax(fax).length >= 10 &&
unformatFax(fax).length <= 11) ||
invalidPhoneLength(10, 11),
validateFax: (fax?: string) => {
if (!fax) return;

const filteredFax = removeNonNumericValues(fax);
return (
filteredFax.length === 0 ||
(filteredFax.length >= 10 && filteredFax.length <= 20) ||
invalidPhoneLength(10, 20)
);
},
},
},
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
requiredMessage,
} from "../../../../../common/helpers/validationMessages";
import { CountryAndProvince } from "../../../../../common/components/form/CountryAndProvince";
import { removeNonNumericValues } from "../../../../../common/helpers/removeNonNumericValues";

/**
* Reusable form for editing user information.
Expand Down Expand Up @@ -84,9 +85,14 @@ export const ReusableUserInfoForm = ({
rules: {
required: { value: true, message: requiredMessage() },
validate: {
validatePhone1: (phone: string) =>
(phone.length >= 10 && phone.length <= 20) ||
invalidPhoneLength(10, 20),
validatePhone1: (phone: string) => {
const filteredPhone = removeNonNumericValues(phone);
return (
(filteredPhone.length >= 10 &&
filteredPhone.length <= 20) ||
invalidPhoneLength(10, 20)
);
},
},
},
label: "Primary Phone",
Expand Down Expand Up @@ -122,14 +128,17 @@ export const ReusableUserInfoForm = ({
rules: {
required: false,
validate: {
validatePhone2: (phone2?: string) =>
phone2 == null ||
phone2 === "" ||
(phone2 != null &&
phone2 !== "" &&
phone2.length >= 10 &&
phone2.length <= 20) ||
invalidPhoneLength(10, 20),
validatePhone2: (phone?: string) => {
if (!phone) return;

const filteredPhone = removeNonNumericValues(phone);
return (
filteredPhone.length === 0 ||
(filteredPhone.length >= 10 &&
filteredPhone.length <= 20) ||
invalidPhoneLength(10, 20)
);
},
},
},
label: "Alternate Phone",
Expand Down Expand Up @@ -164,14 +173,16 @@ export const ReusableUserInfoForm = ({
rules: {
required: false,
validate: {
validateFax: (fax?: string) =>
fax == null ||
fax === "" ||
(fax != null &&
fax !== "" &&
fax.length >= 10 &&
fax.length <= 20) ||
invalidPhoneLength(10, 20),
validateFax: (fax?: string) => {
if (!fax) return;

const filteredFax = removeNonNumericValues(fax);
return (
filteredFax.length === 0 ||
(filteredFax.length >= 10 && filteredFax.length <= 20) ||
invalidPhoneLength(10, 20)
);
},
},
},
label: "Fax",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
invalidPhoneLength,
requiredMessage,
} from "../../../../../../common/helpers/validationMessages";
import { removeNonNumericValues } from "../../../../../../common/helpers/removeNonNumericValues";

export const CompanyContactDetailsForm = ({
feature,
Expand Down Expand Up @@ -49,9 +50,13 @@ export const CompanyContactDetailsForm = ({
rules: {
required: { value: true, message: requiredMessage() },
validate: {
validatePhone: (phone: string) =>
(phone.length >= 10 && phone.length <= 20) ||
invalidPhoneLength(10, 20),
validatePhone: (phone: string) => {
const filteredPhone = removeNonNumericValues(phone);
return (
(filteredPhone.length >= 10 && filteredPhone.length <= 20) ||
invalidPhoneLength(10, 20)
);
},
},
},
label: "Phone",
Expand Down Expand Up @@ -87,14 +92,16 @@ export const CompanyContactDetailsForm = ({
rules: {
required: false,
validate: {
validateFax: (fax?: string) =>
fax == null ||
fax === "" ||
(fax != null &&
fax !== "" &&
fax.length >= 10 &&
fax.length <= 20) ||
invalidPhoneLength(10, 20),
validateFax: (fax?: string) => {
if (!fax) return;

const filteredFax = removeNonNumericValues(fax);
return (
filteredFax.length === 0 ||
(filteredFax.length >= 10 && filteredFax.length <= 20) ||
invalidPhoneLength(10, 20)
);
},
},
},
label: "Fax",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
invalidPhoneLength,
requiredMessage,
} from "../../../../../../common/helpers/validationMessages";
import { removeNonNumericValues } from "../../../../../../common/helpers/removeNonNumericValues";

export const CompanyPrimaryContactForm = ({ feature }: { feature: string }) => (
<div className="company-primary-contact-form">
Expand Down Expand Up @@ -74,9 +75,13 @@ export const CompanyPrimaryContactForm = ({ feature }: { feature: string }) => (
rules: {
required: { value: true, message: requiredMessage() },
validate: {
validatePhone1: (phone: string) =>
(phone.length >= 10 && phone.length <= 20) ||
invalidPhoneLength(10, 20),
validatePhone1: (phone: string) => {
const filteredPhone = removeNonNumericValues(phone);
return (
(filteredPhone.length >= 10 && filteredPhone.length <= 20) ||
invalidPhoneLength(10, 20)
);
},
},
},
label: "Primary Phone",
Expand Down Expand Up @@ -112,14 +117,16 @@ export const CompanyPrimaryContactForm = ({ feature }: { feature: string }) => (
rules: {
required: false,
validate: {
validatePhone2: (phone2?: string) =>
phone2 == null ||
phone2 === "" ||
(phone2 != null &&
phone2 !== "" &&
phone2.length >= 10 &&
phone2.length <= 20) ||
invalidPhoneLength(10, 20),
validatePhone2: (phone?: string) => {
if (!phone) return;

const filteredPhone = removeNonNumericValues(phone);
return (
filteredPhone.length === 0 ||
(filteredPhone.length >= 10 && filteredPhone.length <= 20) ||
invalidPhoneLength(10, 20)
);
},
},
},
label: "Alternate Phone",
Expand Down
44 changes: 35 additions & 9 deletions frontend/src/features/permits/components/form/ContactDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { PHONE_WIDTH, EXT_WIDTH } from "../../../../themes/orbcStyles";
import { BANNER_MESSAGES } from "../../../../common/constants/bannerMessages";
import isEmail from "validator/lib/isEmail";
import { removeNonNumericValues } from "../../../../common/helpers/removeNonNumericValues";

export const ContactDetails = ({ feature }: { feature: string }) => {
return (
Expand Down Expand Up @@ -54,9 +55,14 @@ export const ContactDetails = ({ feature }: { feature: string }) => {
rules: {
required: { value: true, message: requiredMessage() },
validate: {
validatePhone: (phone: string) =>
(phone.length >= 10 && phone.length <= 20) ||
invalidPhoneLength(10, 20),
validatePhone1: (phone: string) => {
const filteredPhone = removeNonNumericValues(phone);
return (
(filteredPhone.length >= 10 &&
filteredPhone.length <= 20) ||
invalidPhoneLength(10, 20)
);
},
},
},

Expand Down Expand Up @@ -95,11 +101,17 @@ export const ContactDetails = ({ feature }: { feature: string }) => {
rules: {
required: false,
validate: {
validatePhone: (phone?: string) =>
!phone ||
phone.length === 0 ||
(phone.length >= 10 && phone.length <= 20) ||
invalidPhoneLength(10, 20),
validatePhone2: (phone?: string) => {
if (!phone) return;

const filteredPhone = removeNonNumericValues(phone);
return (
filteredPhone.length === 0 ||
(filteredPhone.length >= 10 &&
filteredPhone.length <= 20) ||
invalidPhoneLength(10, 20)
);
},
},
},
label: "Alternate Number",
Expand Down Expand Up @@ -172,7 +184,21 @@ export const ContactDetails = ({ feature }: { feature: string }) => {
feature={feature}
options={{
name: "permitData.contactDetails.fax",
rules: { required: false },
rules: {
required: false,
validate: {
validateFax: (fax?: string) => {
if (!fax) return;

const filteredFax = removeNonNumericValues(fax);
return (
filteredFax.length === 0 ||
(filteredFax.length >= 10 && filteredFax.length <= 20) ||
invalidPhoneLength(10, 20)
);
},
},
},
label: "Fax",
width: PHONE_WIDTH,
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
requiredMessage,
} from "../../../../../common/helpers/validationMessages";
import { isValidTransaction } from "../../../helpers/payment";
import { removeNonNumericValues } from "../../../../../common/helpers/removeNonNumericValues";

const FEATURE = "void-permit";

Expand Down Expand Up @@ -175,14 +176,17 @@ export const VoidPermitForm = ({
rules: {
required: false,
validate: {
validateFax: (fax?: string) =>
fax == null ||
fax === "" ||
(fax != null &&
fax !== "" &&
fax.length >= 10 &&
fax.length <= 20) ||
invalidPhoneLength(10, 20),
validateFax: (fax?: string) => {
if (!fax) return;

const filteredFax = removeNonNumericValues(fax);
return (
filteredFax.length === 0 ||
(filteredFax.length >= 10 &&
filteredFax.length <= 20) ||
invalidPhoneLength(10, 20)
);
},
},
},
label: "Fax",
Expand Down
Loading

0 comments on commit 2d9bd63

Please sign in to comment.