2026-06-21 14:00:47 +00:00

43 lines
1.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
public function validateGhanaPhone($phone) {
// Remove spaces, dashes, etc.
$phone = preg_replace('/\D+/', '', $phone);
// If it starts with 0 (e.g. 0241234567), strip the leading 0
if (strpos($phone, '0') === 0) {
$phone = substr($phone, 1);
}
// If it starts with Ghana country code without + (233...), add +
if (strpos($phone, '233') === 0) {
$phone = '+'.$phone;
}
// If it doesnt start with +233, prepend it
if (strpos($phone, '+233') !== 0) {
$phone = '+233'.$phone;
}
// Now validate length: Ghana mobile numbers are 9 digits after +233
$pattern = '/^\+233\d{9}$/';
if (preg_match($pattern, $phone)) {
return $phone; // valid, normalized
} else {
return false; // invalid
}
}
}