Jump to content

How add pattern attribute to address form -> tel field ?


ADBITO.PL

Recommended Posts

  • 1 year later...
On 12/21/2022 at 7:04 PM, endriu107 said:

Please be more specific.

Hi, I have the same question but with another field, the VAT number.

Let's say a VAT number is P49265567.

Sometimes clients imput it as P49265567 (correct), P 49265567 (space between), P-49265567, etcetera.

I want to force them to input it as one letter and eight numbers.

How can i achieve that?

Much thanks!

Link to comment
Share on other sites

I think you need to edit at least two classes in core.

In Validate class you must add new pattern like:

public static function validateVatNumber($vat) {
    $pattern = '/^[a-zA-Z]\d+$/';

    if (preg_match($pattern, $vat)) {
        return true;
    } else {
        return false;
    }
}

and in Address class you need to change validation method from isGenericName to validateVatNumber for vat_number. I'm not tested it and maybe there is need more changes in other places too.

Link to comment
Share on other sites

On 8/22/2024 at 3:26 PM, endriu107 said:

I think you need to edit at least two classes in core.

In Validate class you must add new pattern like:

public static function validateVatNumber($vat) {
    $pattern = '/^[a-zA-Z]\d+$/';

    if (preg_match($pattern, $vat)) {
        return true;
    } else {
        return false;
    }
}

and in Address class you need to change validation method from isGenericName to validateVatNumber for vat_number. I'm not tested it and maybe there is need more changes in other places too.

Hi, I will try to follow the indications, thanks!

Link to comment
Share on other sites

On 12/19/2022 at 8:20 PM, ADBITO.PL said:

Hi
Can anyone help me ?

How to add pattern attribute to address form -> tel field ?

prestashop 1.7.7.8

Hi, what I did for VAT, that can also apply to telephone is:

in /classes/Validate.php add a new function:

	/**
     * Función para validar dnis y cifs. 
     *
     * @param string $dnicif número de dni cif to validate
     *
     * @return bool Validity is ok or not
     */
    public static function isDNICIF($dnicif)
    {
        return preg_match('/(ES)?[0-9a-zA-Z][0-9]{7}[0-9a-zA-Z]|(PT)?[0-9]{9}|(FR)?[0-9a-zA-Z]{2}[0-9]{9}(DE)?[0-9]{9}|(IT)?[0-9]{11}/', $dnicif);
    }

the regex validation is done for multiple countries format. full list from https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s21.html

^(
(AT)?U[0-9]{8} |                              # Austria
(BE)?0[0-9]{9} |                              # Belgium
(BG)?[0-9]{9,10} |                            # Bulgaria
(CY)?[0-9]{8}L |                              # Cyprus
(CZ)?[0-9]{8,10} |                            # Czech Republic
(DE)?[0-9]{9} |                               # Germany
(DK)?[0-9]{8} |                               # Denmark
(EE)?[0-9]{9} |                               # Estonia
(EL|GR)?[0-9]{9} |                            # Greece
(ES)?[0-9A-Z][0-9]{7}[0-9A-Z] |               # Spain
(FI)?[0-9]{8} |                               # Finland
(FR)?[0-9A-Z]{2}[0-9]{9} |                    # France
(GB)?([0-9]{9}([0-9]{3})?|[A-Z]{2}[0-9]{3}) | # United Kingdom
(HU)?[0-9]{8} |                               # Hungary
(IE)?[0-9]S[0-9]{5}L |                        # Ireland
(IT)?[0-9]{11} |                              # Italy
(LT)?([0-9]{9}|[0-9]{12}) |                   # Lithuania
(LU)?[0-9]{8} |                               # Luxembourg
(LV)?[0-9]{11} |                              # Latvia
(MT)?[0-9]{8} |                               # Malta
(NL)?[0-9]{9}B[0-9]{2} |                      # Netherlands
(PL)?[0-9]{10} |                              # Poland
(PT)?[0-9]{9} |                               # Portugal
(RO)?[0-9]{2,10} |                            # Romania
(SE)?[0-9]{12} |                              # Sweden
(SI)?[0-9]{8} |                               # Slovenia
(SK)?[0-9]{10}                                # Slovakia
)$

Then, go to /classes/Address.php, find "public static $definition = [", and change the validation function to the new one

    public static $definition = [
        'table' => 'address',
        'primary' => 'id_address',
        'fields' => [
			(... other fields...)
            'vat_number' => ['type' => self::TYPE_STRING, 'validate' => 'isDNICIF'],
            (... other fields...)
        ],
    ];

 

Link to comment
Share on other sites

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