I am working with a Trekking website and i have used Zend Framework 2. Any visitor for this site can book Trekking Packages from any country. So my client wants Phone number validation based on country. So, i have gone through research and till not found a perfect solution. It would be a great help if any one can help me solving this Phone No. validation work.

有帮助吗?

解决方案

You will need the intl php extention in order for this to work.

After reading the class PhoneNumber.php under Zend\I18n\Validator. I came up with this.

$post is the form post where the user sends country e.g. GB, the country code e.g. 44, 0044, +44, and the phone number. if i remember correctly the class handles 00 and + but you should confirm.

$phone = new \Zend\I18n\Validator\PhoneNumber();//phone validation $phone->setCountry($post->country);//set the country (e.g CY)

switch ($post) {

case !$phone->isValid($post->phone_country_code.$post->phone, array('national'))://check if country and phone are valid

$msg = 'The Phone you Entered is Not valid. Please check the country code and phone and try again.';

break;

default:

$msg=null;

break; }

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top