How to validate postal code by country id?

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
6
down vote
favorite
In Magento checkout there is a postal code verification for each country, now I want to implement it in different module for different case, how to do it if I already have the country id and postal code as the parameter, for example:
$postalCode = '15433';
$countryId = 'UK';
$this->validatePostalCode($countryId,$postalCode);
public function validatePostalCode($countryId,$postalCode)
// how?
if($valid) return true;
else return false;
magento2 shipping-address country-regions
add a comment |Â
up vote
6
down vote
favorite
In Magento checkout there is a postal code verification for each country, now I want to implement it in different module for different case, how to do it if I already have the country id and postal code as the parameter, for example:
$postalCode = '15433';
$countryId = 'UK';
$this->validatePostalCode($countryId,$postalCode);
public function validatePostalCode($countryId,$postalCode)
// how?
if($valid) return true;
else return false;
magento2 shipping-address country-regions
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
In Magento checkout there is a postal code verification for each country, now I want to implement it in different module for different case, how to do it if I already have the country id and postal code as the parameter, for example:
$postalCode = '15433';
$countryId = 'UK';
$this->validatePostalCode($countryId,$postalCode);
public function validatePostalCode($countryId,$postalCode)
// how?
if($valid) return true;
else return false;
magento2 shipping-address country-regions
In Magento checkout there is a postal code verification for each country, now I want to implement it in different module for different case, how to do it if I already have the country id and postal code as the parameter, for example:
$postalCode = '15433';
$countryId = 'UK';
$this->validatePostalCode($countryId,$postalCode);
public function validatePostalCode($countryId,$postalCode)
// how?
if($valid) return true;
else return false;
magento2 shipping-address country-regions
magento2 shipping-address country-regions
edited Sep 13 at 4:18
Bhakti Thakkar
36412
36412
asked Sep 13 at 4:02
nortonuser
37111
37111
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
This should work:
public function validatePostalCode($countryId,$postalCode)
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$postCodeValidator = $objectManager->create(MagentoDirectoryModelCountryPostcodeValidatorInterface::class);
if (!$postCodeValidator->validate($postalCode,$countryId))
return false
else return true;
1
Direct use of object manager is not recommended, kindly use constructor dependency injection instead.
â Anshu Mishra
Sep 13 at 5:31
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
This should work:
public function validatePostalCode($countryId,$postalCode)
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$postCodeValidator = $objectManager->create(MagentoDirectoryModelCountryPostcodeValidatorInterface::class);
if (!$postCodeValidator->validate($postalCode,$countryId))
return false
else return true;
1
Direct use of object manager is not recommended, kindly use constructor dependency injection instead.
â Anshu Mishra
Sep 13 at 5:31
add a comment |Â
up vote
3
down vote
accepted
This should work:
public function validatePostalCode($countryId,$postalCode)
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$postCodeValidator = $objectManager->create(MagentoDirectoryModelCountryPostcodeValidatorInterface::class);
if (!$postCodeValidator->validate($postalCode,$countryId))
return false
else return true;
1
Direct use of object manager is not recommended, kindly use constructor dependency injection instead.
â Anshu Mishra
Sep 13 at 5:31
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
This should work:
public function validatePostalCode($countryId,$postalCode)
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$postCodeValidator = $objectManager->create(MagentoDirectoryModelCountryPostcodeValidatorInterface::class);
if (!$postCodeValidator->validate($postalCode,$countryId))
return false
else return true;
This should work:
public function validatePostalCode($countryId,$postalCode)
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$postCodeValidator = $objectManager->create(MagentoDirectoryModelCountryPostcodeValidatorInterface::class);
if (!$postCodeValidator->validate($postalCode,$countryId))
return false
else return true;
answered Sep 13 at 5:03
Idham Choudry
1,1541630
1,1541630
1
Direct use of object manager is not recommended, kindly use constructor dependency injection instead.
â Anshu Mishra
Sep 13 at 5:31
add a comment |Â
1
Direct use of object manager is not recommended, kindly use constructor dependency injection instead.
â Anshu Mishra
Sep 13 at 5:31
1
1
Direct use of object manager is not recommended, kindly use constructor dependency injection instead.
â Anshu Mishra
Sep 13 at 5:31
Direct use of object manager is not recommended, kindly use constructor dependency injection instead.
â Anshu Mishra
Sep 13 at 5:31
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f242036%2fhow-to-validate-postal-code-by-country-id%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password