Jump to content

[SOLVED] Phone number fixed length


Recommended Posts

Hi,

 

In my country our phone number has 10 digits. Some customer input a shorter , or longer number, and this is frustrating. Sometimes the customer checks his e-mail once every 2-3 days, and i need him to confirm the order as soon as possible. So my question is : How can i make the phone number field ask for a 10 digits number, not a minimum number or maximum number but just 10 digits. I tried to use the minimum 5 digits code from password inside the Validate.php and in the database to edit the 16 characters default length but it did not work :( I'm still a noob at this, so i will really appreciate your help :D

 

PS version 1.5.5.0

Edited by okaty (see edit history)
Link to comment
Share on other sites

classes/Validate.php
 
 
instead of this:
	public static function isPhoneNumber($number)
	{
		return preg_match('/^[+0-9. ()-]*$/', $number);
	}

use this:

 
public static function isPhoneNumber($number){
  if (strlen($number)==10){
    return preg_match('/^[+0-9. ()-]*$/', $number);
  }else{
   return false;
  }
}

unfortunately without core change it isn't possible, solution is above :)

  • Like 3
Link to comment
Share on other sites

Thank you!

 

It works perfectly!

 

Vekia is there a way to change the validate.php file in themes so when i update the php file will preserve ?

 

[sOLVED]

 

you're welcome B)

 

and what you exactly mean by your second question? Sorry but i don't know what you want to achieve, can you shed some light on it please?

Link to comment
Share on other sites

  • 2 years later...

i tried that on 1.5.3.1 and it doesn't work. when fields are different from 10 it gives an error message saying phone is invalid. when it has 10 digits it gives an error 500 server error.

 

also, how do i configure to have at least 6 digits, instead of 10 digits exactly?

 

i have one phone number required (home or mobile) but if i use "space" on one of them, it accepts the input. and i don't want that :(

 

can you guys help me?

Edited by patuga (see edit history)
Link to comment
Share on other sites

  • 10 months later...

Hi!

 

Can anybody tell me the regex expression for phone numbers with a backslash in it?

 

For example 12345/56778 should be allowed.

 

This seems not to work...

 

public static function isPhoneNumber($number)
    {
        return preg_match('/^[+0-9. ()-/]*$/', $number);
    }
  • Like 1
Link to comment
Share on other sites

  • 6 months later...

 

classes/Validate.php
 
 
instead of this:
	public static function isPhoneNumber($number)
	{
		return preg_match('/^[+0-9. ()-]*$/', $number);
	}

use this:

 
public static function isPhoneNumber($number){
  if (strlen($number)==10){
    return preg_match('/^[+0-9. ()-]*$/', $number);
  }else{
   return false;
  }
}

unfortunately without core change it isn't possible, solution is above :)

 

Hello Vekia

 

And about different sizes of phone number... like 11 numbers for mobile and 10 for home phone?

Link to comment
Share on other sites

And about different sizes of phone number... like 11 numbers for mobile and 10 for home phone?

public static function isPhoneNumber($number){
  $len = strlen($number);
  $min = 10;
  $max = 11;
  if ($len >= $min && $len <= $max){
    return preg_match('/^[+0-9. ()-]*$/', $number);
  }else{
   return false;
  }
}

this will check that phone number as at least $min characters and at most $max characters. Feel free to change min/max constants values

  • Like 1
Link to comment
Share on other sites

I made tests and works.

 

But for my case, i need that only numbers can be inserted. i need 11 numbers for mobile and 10 for home phone

 

Because i see that the strings ()- are acceptable as counting on total number on field phone number.

Link to comment
Share on other sites

I made tests and works.

 

But for my case, i need that only numbers can be inserted. i need 11 numbers for mobile and 10 for home phone

 

Because i see that the strings ()- are acceptable as counting on total number on field phone number.

 

Then try this one instead

public static function isPhoneNumber($number){
  $stripped = preg_replace('/[^0-9]/', '', $number);
  $len = strlen($stripped);
  $min = 10;
  $max = 11;
  if ($len >= $min && $len <= $max){
    return preg_match('/^[+0-9. ()-]*$/', $number);
  }else{
   return false;
  }
}

It still accepts phone numbers like "1-234-561-239" or "(123) 450 1111", but only if number of digits is 10 or 11.   

  • Like 1
Link to comment
Share on other sites

Then try this one instead

public static function isPhoneNumber($number){
  $stripped = preg_replace('/[^0-9]/', '', $number);
  $len = strlen($stripped);
  $min = 10;
  $max = 11;
  if ($len >= $min && $len <= $max){
    return preg_match('/^[+0-9. ()-]*$/', $number);
  }else{
   return false;
  }
}

It still accepts phone numbers like "1-234-561-239" or "(123) 450 1111", but only if number of digits is 10 or 11.   

 

 

Thank you very much Datakick.

 

Works great.

 

Where is the button to your tip please.

 

Rita

Link to comment
Share on other sites

×
×
  • Create New...