How to use regular expression check to compare values of two custom fields?

  • Posts: 9
  • Thank you received: 0
2 days 23 hours ago #367523

Hi,

If there are two custom fields of Text type, address_telephone1 and address_telephone2. how can I use regular expression to check if the value of
address_telephone2 equals the value of address_telephone1? I want to make sure users input the correct telephone number.

Thank you!

Please Log in or Create an account to join the conversation.

  • Posts: 83933
  • Thank you received: 13590
  • MODERATOR
2 days 13 hours ago #367524

Hi,

You can't do that with a regular expression check. A regular expression allows you to check the format of the value entered in a field. The regular expression doesn't have access to the value of other fields.

I'm afraid it will require some coding, one way or another.

One solution could be to:
- use the "display view files" setting of the HikaShop configuration to identify which view file(s) is used to display the fields :
www.hikashop.com/support/documentation/5...g.html#advanced_main
- edit the view file(s) via the menu Display>Views.
- in there, add custom javascript code to make that check:

<span id="error-message" style="color: red; display: none;">Telephone numbers do not match.</span>
<script>
document.addEventListener('DOMContentLoaded', function () {
  const tel1 = document.getElementById('address_telephone1');
  const tel2 = document.getElementById('address_telephone2');
  const errorMessage = document.getElementById('error-message');

  function checkTelephoneMatch() {
    if (tel1.value && tel2.value) {
      if (tel1.value !== tel2.value) {
        errorMessage.style.display = 'inline';
        return false;
      } else {
        errorMessage.style.display = 'none';
        return true;
      }
    }
    errorMessage.style.display = 'none';
    return true;
  }

  // check on input change
  tel1.addEventListener('input', checkTelephoneMatch);
  tel2.addEventListener('input', checkTelephoneMatch);
});
</script>
Note that this is a simplified solution. I will only add a message to the user if the value in each field are not the same but it won't block the user from proceeding. A proper solution would also have to block the form submission where these fields are if they are not filled with the same text. So ideally, the code would have to be modified in each view file to adapt to the form there to handle that and it is not that simple.

A better solution, supposing that both fields are of the table "address", would be to implement the onBeforeAddressCreate and onBeforeAddressUpdate events of the HikaShop address API in a plugin of the group "hikashop":
www.hikashop.com/support/documentation/6...ntation.html#address
In them, you could compare both fields in $element->address_telephone1 and $element->address_telephone2 and if different, set $do to false and add an error message with Joomla's error message system:
docs.joomla.org/display_error_messages_and_notices

This could help:
chatgpt.com/share/6843597c-b19c-8003-a3c3-8a84c7ea7fb2

Please Log in or Create an account to join the conversation.

Time to create page: 0.057 seconds
Powered by Kunena Forum