Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added phone number validation in PHP #139

Merged
merged 2 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,8 @@ Programming Language: Java, C, HTML, CSS, Kotlin
Email: [email protected]


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: [email protected]

44 changes: 44 additions & 0 deletions telephone-validator/validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
** Phone number validator
** based on the JS validator regex.
** In this example, 2-123-123-1231 will return true
*/

class PhoneNumber
{
protected $phoneNumber;

protected $regex = '/^\d(?:-\d{3}){3}\d$/';

/**
* PhoneNumber constructor.
* @param $phoneNumber
*/
public function __construct($phoneNumber)
{
$this->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 :-(';