generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ORV2-3042 - contact info field validation (#1671)
Co-authored-by: GlenAOT <[email protected]> Co-authored-by: zgong-gov <[email protected]>
- Loading branch information
1 parent
4d05079
commit e3371b4
Showing
19 changed files
with
240 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Filter out any non-digit characters from a string. | ||
* @param input Any string input | ||
* @returns String containing only digits | ||
*/ | ||
export const filterNonDigits = (input: string) => | ||
input.replace(/[^0-9]/g, ""); |
49 changes: 49 additions & 0 deletions
49
frontend/src/common/helpers/phone/getFormattedPhoneNumber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Nullable } from "../../types/common"; | ||
import { filterNonDigits } from "../numeric/filterNonDigits"; | ||
|
||
/** | ||
* Get the formatted phone number from a provided phone number string. | ||
* @param input Inputted string that could contain phone number | ||
* @returns Formatted phone number | ||
*/ | ||
export const getFormattedPhoneNumber = (input?: Nullable<string>): string => { | ||
if (!input) return ""; | ||
|
||
// Only accept digits as part of phone numbers | ||
const parsedPhoneDigits: string = filterNonDigits(input); | ||
const phoneDigitsLength = parsedPhoneDigits.length; | ||
|
||
// Ignore formatting if the value length is greater than a standard Canada/US phone number | ||
// (11 digits including country code) | ||
if (phoneDigitsLength > 11) { | ||
return parsedPhoneDigits; | ||
} | ||
|
||
// If there are no digits in the resulting parsed phone number, return "" | ||
if (phoneDigitsLength < 1) return parsedPhoneDigits; | ||
|
||
// If there are 1-3 digits in the parsed phone number, return them as is | ||
// ie. "x", "xx", or "xxx" | ||
if (phoneDigitsLength < 4) return `${parsedPhoneDigits.slice(0, 3)}`; | ||
|
||
// If there are 4-6 digits in the parsed phone number, return the first 3 digits as area code (in brackets followed by space) | ||
// followed by the rest of the digits as just digits with no formatting | ||
// ie. "(xxx) x", "(xxx) xx", "(xxx) xxx", | ||
if (phoneDigitsLength < 7) | ||
return `(${parsedPhoneDigits.slice(0, 3)}) ${parsedPhoneDigits.slice(3)}`; | ||
|
||
// If there are 7-10 digits, return the first 6 digits based on the above formatting rules, | ||
// followed by a dash and the remaining digits will be unformatted | ||
// ie. "(xxx) xxx-x", "(xxx) xxx-xx", "(xxx) xxx-xxx", "(xxx) xxx-xxxx" | ||
if (phoneDigitsLength < 11) | ||
return `(${parsedPhoneDigits.slice(0, 3)}) ${parsedPhoneDigits.slice( | ||
3, | ||
6, | ||
)}-${parsedPhoneDigits.slice(6, 10)}`; | ||
|
||
// With exactly 11 digits, format the phone number like this: "+x (xxx) xxx-xxxx" | ||
return `+${parsedPhoneDigits.slice(0, 1)} (${parsedPhoneDigits.slice( | ||
1, | ||
4, | ||
)}) ${parsedPhoneDigits.slice(4, 7)}-${parsedPhoneDigits.slice(7, 11)}`; | ||
}; |
13 changes: 13 additions & 0 deletions
13
frontend/src/common/helpers/phone/validateOptionalPhoneNumber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Nullable } from "../../types/common"; | ||
import { validatePhoneNumber } from "./validatePhoneNumber"; | ||
|
||
/** | ||
* Validate an optional phone number. | ||
* @param phone Provided phone number, if any | ||
* @returns true if phone number is valid or empty, error message otherwise | ||
*/ | ||
export const validateOptionalPhoneNumber = (phone?: Nullable<string>) => { | ||
if (!phone) return true; // phone number is optional, so empty is accepted | ||
|
||
return validatePhoneNumber(phone); | ||
}; |
16 changes: 16 additions & 0 deletions
16
frontend/src/common/helpers/phone/validatePhoneExtension.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Nullable } from "../../types/common"; | ||
import { invalidExtension, invalidExtensionLength } from "../validationMessages"; | ||
|
||
/** | ||
* Validate optional phone extension. | ||
* @param ext Provided phone extension, if any | ||
* @returns true if phone extension is valid, error message otherwise | ||
*/ | ||
export const validatePhoneExtension = (ext?: Nullable<string>) => { | ||
if (!ext) return true; // empty or not-provided phone extension is acceptable | ||
|
||
if (ext.length > 5) return invalidExtensionLength(5); | ||
|
||
// Must have exactly 1-5 digits | ||
return /^[0-9]{1,5}$/.test(ext) || invalidExtension(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { filterNonDigits } from "../numeric/filterNonDigits"; | ||
import { invalidPhoneLength } from "../validationMessages"; | ||
|
||
/** | ||
* Validate phone number. | ||
* @param phone Provided phone number to validate | ||
* @returns true if phone number is valid, otherwise returns error message | ||
*/ | ||
export const validatePhoneNumber = (phone: string) => { | ||
const filteredPhone = filterNonDigits(phone); | ||
return ( | ||
(filteredPhone.length >= 10 && | ||
filteredPhone.length <= 20) || | ||
invalidPhoneLength(10, 20) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.