From 683276420869078f401271543edb6ad079b571c0 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 1 Oct 2018 20:12:28 +0200 Subject: [PATCH] Added phone number validation in PHP --- CONTRIBUTORS.md | 7 ++++- telephone-validator/validator.php | 44 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 telephone-validator/validator.php diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 1e6bd69e..bb8dd582 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -62,4 +62,9 @@ Name: [Joseph Lee](https://github.com/jjlee050) Place: Singapore, SG About: I am currently a NUS undergraduate Year 2 Computer Science Student. My field of interest is towards Software Engineering and Mobile App Development. Programming Language: Java, C, HTML, CSS, Kotlin -Email: josephlee050@gmail.com \ No newline at end of file +Email: josephlee050@gmail.com + +Name: [John Braun](https://github.com/jhnbrn90) +About: I am doing a PhD in organic chemistry and really like programming. Used Laravel to create some useful tools for our research group, amongst others a chemicals inventory, booking system and supporting information manager. +Programming Language: PHP, JavaScript +Email: jbraunnl@gmail.com diff --git a/telephone-validator/validator.php b/telephone-validator/validator.php new file mode 100644 index 00000000..75dcc85e --- /dev/null +++ b/telephone-validator/validator.php @@ -0,0 +1,44 @@ +phoneNumber = $phoneNumber; + } + + /** + * Validate the passed in phone number + * using the defined regex + * @return bool + */ + public function validate() + { + preg_match($this->regex, $this->phoneNumber, $matches); + return !empty($matches); + } +} + +// Register a new phone number +$phoneNumber = new PhoneNumber('2-123-123-1231'); + +// validate the phone number, returning a boolean +$phoneNumber->validate(); + +// return a statement (string) based on the boolean value +echo $phoneNumber->validate() ? 'Valid phone number :-)' : 'Invalid phone number :-('; +