Skip to content

Conversation

@Claudye
Copy link
Contributor

@Claudye Claudye commented May 9, 2023

Users will now have the option to specify an optional parameter when instantiating the ValidPhoneNumber class, which will allow them to validate phone numbers for a specific country. If no parameter is specified, the default country will be used for validation.

Copy link
Owner

@milwad-dev milwad-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you say how to validate phone number with country code, where is section about validate phone (Regex, or ...)!?

@Claudye
Copy link
Contributor Author

Claudye commented May 10, 2023

I wrote a class

Milwad\LaravelValidate\Utils\CountryPhoneCallback::class

that contains a list of methods to validate phone numbers according to the convention. Each method is named validateCODE() and returns a boolean. This class allows writing any valid PHP code (not just a regex) to validate phone numbers.

In addition to that, the class contains a method called callPhoneValidator() which allows to call the appropriate method to validate a phone number according to the provided country code.

This class takes as parameters in it constructor the phone number or a comma separated list of phone numbers, the ISO country code, and the attribute.
It is called in the ValidPhoneNumber class if a $code parameter has been passed to it, otherwise it executes the regular expression that was there

Ex:

public  function  phone_number_is_valid() {
	$rules  = [
	'phone_number' => [new  ValidPhoneNumber()],
	'phone_bj' => [new  ValidPhoneNumber(Country::BENIN)]
	];
	$data  = ['phone_number' => '09366000000', 'phone_bj' => "+22997000000"];
	$passes  =  $this->app['validator']->make($data, $rules)->passes();
	$this->assertTrue($passes);
}

@milwad-dev
Copy link
Owner

For example, how to validate Iran phone number? where is it!

@milwad-dev milwad-dev changed the title Validate phone by country code [1.x] Validate phone by country code May 10, 2023
Copy link
Owner

@milwad-dev milwad-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🧡!

@milwad-dev milwad-dev merged commit eb5eae3 into milwad-dev:1.x May 10, 2023
@Claudye
Copy link
Contributor Author

Claudye commented May 10, 2023

I don't know the phone number format of all countries. I did first if accepted I will add for other countries

@milwad-dev
Copy link
Owner

Is important to add it soon!

@Claudye
Copy link
Contributor Author

Claudye commented May 10, 2023

I work on already

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants