Phone Regex Validation Php
phone regex validation php for production applications
Pattern Views
47,765
↗ 12.5%Last 30 days
Community Rating
⭐
92%
265 likes
Regular Expression
/^\+?1?[-\.\s]?\(?[0-9]{3}\)?[-\.\s]?[0-9]{3}[-\.\s]?[0-9]{4}$/
Explanation
This regex pattern validates phone regex validation php. Commonly used by developers for input validation and form checking.
Code Examples
JavaScript Example
javascript
// phone regex validation php - JavaScript
const regex = /^\+?1?[-\.\s]?\(?[0-9]{3}\)?[-\.\s]?[0-9]{3}[-\.\s]?[0-9]{4}$/;
function validate(input) {
if (!input || typeof input !== 'string') return false;
return regex.test(input.trim());
}
// Usage examples
console.log(validate('555-123-4567')); // true
console.log(validate('123')); // falsePython Example
python
# phone regex validation php - Python
import re
pattern = r"^\+?1?[-\.\s]?\(?[0-9]{3}\)?[-\.\s]?[0-9]{3}[-\.\s]?[0-9]{4}$"
regex = re.compile(pattern)
def validate(input_str):
if not input_str or not isinstance(input_str, str):
return False
return bool(regex.match(input_str.strip()))
# Usage examples
print(validate('555-123-4567')) # True
print(validate('123')) # FalsePHP Example
php
<?php
// phone regex validation php - PHP
function validate($input) {
if (!is_string($input) || empty(trim($input))) {
return false;
}
$pattern = '/^\+?1?[-\.\s]?\(?[0-9]{3}\)?[-\.\s]?[0-9]{3}[-\.\s]?[0-9]{4}$/';
return preg_match($pattern, trim($input)) === 1;
}
// Usage examples
var_dump(validate('555-123-4567')); // bool(true)
var_dump(validate('123')); // bool(false)
?>Test Cases
✓ Should Match
555-123-4567
(555) 123-4567
✗ Should NOT Match
123
not-phone
Performance
Speed
fast
Memory
low