Jump to content

Phone Number Validation


Recommended Posts

    public static function isPhoneNumber(9)
    {
        return preg_match('/^[0-9]{0,9}$/', $number);
    }

 

 

Is that correct now?

I want to do something, that will be requiering to type 9 digits, because now somebody, can write only 1 digit in form...

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

Now its working! Thanks Bro. I have one more question. How can I now edit the message which is informing customer about this form. I mean that i want to do something that will show another error message than the default one. " Please enter full phone number 111-222-333" Or something like that.

Link to comment
Share on other sites

  • 1 year later...

 

replace like this.

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

 

This is not 100% corrert solution.

This code will only check, if there are 9 characters, but not check if all characters are digits.

 

Correct code is 

    public static function isPhoneNumber($number)
    {
        return (preg_match('/^[+0-9. ()-]*$/', $number) AND strlen($number) == 9);
    }
Edited by Petr Pánek (see edit history)
  • Like 2
  • Thanks 2
Link to comment
Share on other sites

  • 3 years later...
On 7/24/2017 at 1:48 PM, Petr Pánek said:

 

This is not 100% corrert solution.

This code will only check, if there are 9 characters, but not check if all characters are digits.

 

Correct code is 


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

This is working! Thanks - anyone know where or how to customise error messages on this validation? At the moment I've got wrong format on error but I would like to add some extra text for clients...

Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...