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.';
}*/
@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.
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); ?>
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.