3

i want to add php phone number validation in opencart code sample is given 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.';
        }*/

3 Answers 3

8

@Tayyab Gulsher Vohra....

I am also facing this problem.I concluded myself and made it. follow the instruction., The opencart is a MVC structure platform,so you need to know about Controller , Model and view ,Here Controller is first initialization telephone number in index()function

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

further, telephone number must be valid using this method.,

 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 telephone number value in language file.

1
  • 1
    Your code is right u can define this support which format like (91)-112-122-1252 ?
    – Ravi Patel
    Commented Oct 16, 2014 at 11:29
2

Well, you have 2 options, validate or filter. Because phone numbers are different in length and characters, what I would suggest is that you just filter

FILTER_SANITIZE_NUMBER_INT

Remove all characters except digits, plus and minus sign.

for more info: Filters

EXAMPLE:

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

Sanitizer functions are not really ready for validation of phone numbers and I would probably use a regex leaving ( ) + - : e and numbers in as this allows for most variations of international numbers and extensions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.