Closed fico7489 closed 6 years ago
Same issue , always false
Guys, a general advice: if you use undocumented methods like isValidFormat()
always review the source code to check the actual functionality.
isValidFormat()
merely checks if the supplied number format is valid (e.g. international
, E.164
, ...), it is NOT meant for phone number validation. So passing a phone number will always return false.
To validate a phone number manually you can just manually create Laravel's built-in Validator. For example:
use Validator;
$phoneNumber = '+17025891156';
$validator = Validator::make(['data' => $phoneNumber], ['data' => 'phone:US']);
$validator->passes(); // TRUE if phone number is a US number
If you don't want to use the Validator, then you can directly interact with libphonenumber
and you don't need this package.
I don't want to use undocumented methods but there is no documented method for manual validation, however, I found I a workaround :
try {
$phone = PhoneNumber::make($value)->getCountry();
//valid
} catch (NumberParseException $e) {
//not valid
}
There's no need to document it as there isn't any dedicated method. You just need to leverage Laravel's Validator class. I'd highly encourage to switch over to my posted snippet instead of relying on your try/catch block. You're missing out edge cases like that.
I will try this, thanks for the response...
Can I validate number manually, I don't see this in the documentation?
Why this is false
dd(PhoneNumber::isValidFormat('+17025891156'));
?