Skip to content

Instantly share code, notes, and snippets.

@sepiariver
Created October 30, 2015 07:12
Show Gist options
  • Save sepiariver/924460da645bc35a6e6a to your computer and use it in GitHub Desktop.
Save sepiariver/924460da645bc35a6e6a to your computer and use it in GitHub Desktop.
FormIt validator for phone number based on: https://regex101.com/r/xX3hO8/1
<?php
$success = false;
if (!empty($value)) {
$re = "/^((((\+[\d\-.]{1,5})?[ \-.]?\d{3})|(\+[\d\-.]{1,5})?[ \-.]?\((\d{3}\)))?[ \-.]?\d{3}[ \-.]?\d{4}\s?(e?x?t?\.?\s?\d{1,7})?)?$/i";
if (preg_match($re, $value) === 1) $success = true;
}
if (!$success) {
$validator->addError($key, 'Please enter a valid phone number.');
return false;
} else {
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment