From 143a4680e25ac4d46bb12bea7efcbef4f7480909 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Sat, 25 Sep 2021 21:56:01 +0200 Subject: [PATCH] Add validation function for phone numbers --- src/MD_STD_IN.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/MD_STD_IN.php b/src/MD_STD_IN.php index 6e53a9f..d76c175 100644 --- a/src/MD_STD_IN.php +++ b/src/MD_STD_IN.php @@ -212,6 +212,27 @@ final class MD_STD_IN { } + /** + * Sanitizes and validates a phone number. An empty string passes. + * + * @param mixed $input Input string. + * + * @return string + */ + public static function validate_phone_number(mixed $input):string { + + if ($input === "") { + return ""; + } + + if (!preg_match("#^[()0-9/ +-]+$#", $input)) { + throw new MDgenericInvalidInputsException("Invalid phone number entered."); + } + + return $input; + + } + /** * Sanitizes a string to a float. *