Regex for username on registration

  • Posts: 6
  • Thank you received: 0
  • Hikashop Essential
10 months 2 weeks ago #352393

-- HikaShop version -- : 4.7.3
-- Joomla version -- : 4.3.2
-- PHP version -- : 8.1
-- Browser(s) name and version -- : Google Chrome

Hi,

I want to put a username Regex on registration. Our Website is connected with Moodle and people buy courses through Hikashop.

When a customer register himself, he can put the username he wants which is quite inconvenient as Moodle is case sensitive and refere to that username.

Can you tell me on which file should I put my Regex please ?

Thanks,

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

  • Posts: 81568
  • Thank you received: 13078
  • MODERATOR
10 months 2 weeks ago #352396

Hi,

Modifying directly a core file of HikaShop or Joomla is not what I would recommend.
There are two ways I would recommend doing it:
- you could develop a small Joomla plugin of the group "user" and implement the event onUserBeforeSave :
docs.joomla.org/Plugin/Events/User#onUserBeforeSave
That way, it works not only with HikaShop's registration form, but Joomla's and most extensions out there which need to have their own registration form. Also, since you're doing it as a plugin, you won't have to modify a core file each time you update Joomla or HikaShop, and it would work even with future versions of Joomla / HikaShop (until Joomla decides to break the plugin system but I hope it never happens)
- you could also do a view override of the registration form view. For HikaShop, you can easily find which view file you need to change :
www.hikashop.com/support/documentation/1...-display.html#layout
In it, you could add a javascript script to monitor what is being written in the username input field. Here is a tutorial on input field validation in javascript:
dev.to/dailydevtips1/vanilla-javascript-email-validation-21b3
Since you would do this as a view override, you won't have issues with updates for a while. However, this means you won't get the modifications we add in that view file in the future (for new features, or compatibility with future versions of Joomla), it's on the browser side of things, so bots or attackers can bypass the check, and it would only apply to the registration form in HikaShop, leaving the Joomla registration form page open with the same issue unless you also do an override for it.

So as you can see, I recommend the plugin approach. It's a bit more work if you're not familiar with how to create a plugin (but once you know how, it's nothing difficult), but in all other aspects, I think it's better.

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

  • Posts: 6
  • Thank you received: 0
  • Hikashop Essential
10 months 1 week ago #352568

Hi Nicolas,

Thanks for you answer.

I did a plugin to check that the username respect my needs.

Here is a link if someone need it : github.com/Bwerpophile/Joomla_Restrictusername_plugin

I'm facing a problem regarding the javascrypt side of the question.

When I try to get the value of the field, it doesn't seems to be store inside the DOM.

Do you have any idea of a work around or where can I get what is input in the field ?

Thanks again for your time,

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

  • Posts: 81568
  • Thank you received: 13078
  • MODERATOR
10 months 1 week ago #352569

Hi,

What JS code are you using ?

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

  • Posts: 6
  • Thank you received: 0
  • Hikashop Essential
10 months 5 days ago #352676

Hi,

Vanilla JS. I tried with this :

   <script type="text/javascript">
   document.addEventListener('DOMContentLoaded', function() {
  var emailField = document.getElementById('register_email');
  var usernameField = document.getElementById('register_username');

  if (emailField && usernameField) {
    usernameField.addEventListener('input', function() {
      var email = emailField.value;
      var username = usernameField.value;

      if (email !== username) {
        usernameField.setCustomValidity('Username must match the email address.');
      } else {
        usernameField.setCustomValidity('');
      }
    });
  }
});

Last edit: 10 months 5 days ago by nicolas.

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

  • Posts: 81568
  • Thank you received: 13078
  • MODERATOR
10 months 5 days ago #352682

Hi,

Try to add the code

var emailField = document.getElementById('register_email');
  var usernameField = document.getElementById('register_username');
inside the event handler.

Note also that if your goal is for your users to have the same username and email address, why not simply set the "registration" setting of the HikaShop configuration to "simplified registration with password". That way, it will remove the username field, and the system will automatically use the email address as username.

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

  • Posts: 6
  • Thank you received: 0
  • Hikashop Essential
10 months 4 days ago #352714

Hi,

All right, that sort everything.

I did not know that Hikashop was putting the email as username on simple registration.

I did that in the end.

Thanks,

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

Time to create page: 0.066 seconds
Powered by Kunena Forum