S
S
strify_252020-11-27 17:21:17
1C-Bitrix
strify_25, 2020-11-27 17:21:17

How to validate the phone when placing an order in Bitrix?

Added a phone input mask, it works correctly. But if the user enters the phone number incompletely, it will remain so, for example, +7 (920) 333-__-__
I try to enter a regular expression in the order properties in the admin panel to check

/^[+][0-9] [(][0-9]{3}[)] [0-9]{3}[-][0-9]{2}[-][0-9]{2}$/


but the message "Order property 'Phone' - does not match the pattern" appears, although the entered value +7 (999) 999-99-99 fully matches the regular expression

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Gritsuk, 2020-11-27
@strify_25

If the mask was made using InputMask , then at the front you can add field validation BEFORE saving the order

if ($(selector).inputmask("isComplete")){
    //do something
}

You can add server field validation to this validation on the sale OnSaleOrderBeforeSaved
module event. The handler might look something like this:
<?
function OnSaleOrderBeforeSaved(\Bitrix\Main\Event $event)
{
    /** @var \Bitrix\Sale\Order $order */
    $order = $event->getParameter("ENTITY");

    if ($order->isNew() ) {//Если проверка требуется только для нового заказа, а не при его редактировании
        $propertyCollection = $order->getPropertyCollection();
        $phoneProperty = $propertyCollection->getPhone();
        $isPhoneValid = validatePhone($phoneProperty);

        if (!$isPhoneValid) {
            $event->addResult(new \Bitrix\Main\EventResult(
                \Bitrix\Main\EventResult::ERROR,
                new \Bitrix\Sale\ResultError('Текст ошибки', 'PHONE_PROPERTY_INVALID')
            ));
        }

    }
}

you only need to implement the validatePhone method / function, which validates the phone as you need

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question