Jump to content

Validating customer names, address and phone


Recommended Posts

So far I have been quite impressed by out of the box features provided by Prestashop but this thing really let me down: If someone wants to register on the site or do a guest check out he can put anything as his name. Anything includes things like "---", all spaces, "***" etc. There is no validation provided here and this is really annoying.

 

I have tried to modify the processSubmitAccount method in website\controllers\front\AuthController.php to use the Validate::isName() method but this does not help at all. It lets the users put all the strange character in the name field. I tried to create another method inside the Validate class:

    public static function isHumanName($name)
    {    
        return preg_match('/^[A-Za-z]+$/', $name);
    }

but for some reason this simple pattern does not work. All I want to be able to do is allow names like "FirstName MiddleName" having only one space chracter at a time and not at the start of the name. My php and regex skills are minimal and this is all I have come up with so far.

 

Besides name I want to validate addresses and phone numbers. Addresses can have numbers in addition to letters while phone numbers can only have numbers. I guess if the problem with validating names is solved then I would be able to do it for address and numbers as well.

 

I would appreciate if someone can put me in right direction.

 

Thanks

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

After some help and debate on StackOverflow I got my solution:

 

preg_match('/^[A-Za-z]+(\s?[A-Za-z]+)*$/', $name);

 

I have put above code in a method in Validate class. I then make use of this method in AuthController.php in processSubmitAccount method:

if (!Validate::isHumanName($_POST['firstname']))
 $this->errors[] = Tools::displayError('Please enter a valid First Name.');

This works for me for now at least!

 

DISCLAIMER: It is not recommended to use the same regex in their production systems. You may use it as starting point but it may not work in your case. Forcing your customers to validate their names is also not recommended.

 

Reference

Link to comment
Share on other sites

  • 9 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...