Check

I want to add php phone number confirmation in the opencart code example below

if(!filter_var($customer_email, FILTER_VALIDATE_EMAIL)) { $errors[$pos++] = 'Email is not valid.'; } if($customer_mobile=='') { $errors[$pos++] = 'Please enter your Mobile.'; } /*elseif (strlen($customer_mobile) < 11 || !is_numeric($this->request->post['cell_number'])){ $errors[$pos++] = 'Mobile number should be 7 digits.'; }*/ 
+4
source share
3 answers

@Tayyab Gulsher Vohra ....

I also run into this problem. I concluded and did it. follow the instructions., The operating system is the platform of the MVC structure , so you need to know about the controller, model and presentation. Here, Controller is the first initialization phone number in the index() function

 if (isset($this->request->post['telephone'])) { $this->data['telephone'] = $this->request->post['telephone']; } else { $this->data['telephone'] = $this->customer->getTelephone(); } 

further, this phone number must be valid,

  function validate() if( !preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $this->request->post['telephone']) ) { $this->error['telephone'] = $this->language->get('error_telephone'); } 

then you should be initialized with the phone number in the language file.

+7
source

You have 2 options, check or filter. Since phone numbers vary in length and characters, I would suggest that you just filter

 FILTER_SANITIZE_NUMBER_INT 

Delete all characters except numbers, plus and minus.

for more information: Filters

An example :

 <?php echo filter_var("Home: +1 (123) 456-7890 @ John", FILTER_SANITIZE_NUMBER_INT); ?> 
+2
source

The sanitizer functions are not really ready to check phone numbers, and I would probably use regex leave () + -: e and numbers, since this allows you to use most options for international numbers and extensions.

+1
source

Source: https://habr.com/ru/post/1443790/


All Articles