Block numbers in usernames at registration

Go down

Tutorial Block numbers in usernames at registration

Post by Ange Tuteur on Mon 21 Jul - 1:40

Block numbers in usernames at registration


This tutorial will allow you to prevent usernames that contain numbers at registration, as in the example below.

Block numbers in usernames at registration Captur53


Setting up

Go to Administration Panel > Modules > JavaScript Codes management and create a new script.

Title : Your choice
Placement : In all the pages
Code:
location.pathname=="/register" && location.search=="?agreed=true&step=2" && $(function(){
        
  var username_format = /^[^0-9]*$/;
  var format_notice = "Numbers are not allowed in usernames";
        
  var f = $('form[action=""][method="post"]');
  var p = $('#username_reg');
  var i = $('<span id="username_issue" />').html('<br/>'+format_notice).css({ "color":"red", "font-style":"italic", "display":"none"}).appendTo(p.parent());
        
  var u = function() {
    if (username_format.test(p.val())) i.hide();
    else {
      i.show();
      return false;
    }
  };
        
  p.on('change keyup', function(){ u() });
  $('input[type="reset"]', f).click(function(){ u() });
  $('input[type="submit"]', f).click(u);
  u();
        
});


Variants

The script uses a regular expres​sion( /^[^0-9]*$/ ) to detect if the username contains a digit.


Here are a few websites with tutorials on regular expressions :

And some examples that could take the place of /^[^0-9]*$/ :

Only allow usernames that contain digits :
Code:
/^[0-9]*$/

Only allow usernames that contain 0 or 1 :
Code:
/^[01]*$/

Only allow usernames that contain 3 words of 3-10 characters ( without a number ) :
Code:
/^(\b[^\d\s]{3,10}\b(\s+|$)){3}$/

Don't allow usernames that contain special characters :
Code:
/^[^!@#$%^&*:;"',.?]*$/


Ange Tuteur
Ange Tuteur
Forumaster

Male Posts : 13223
Reputation : 2961
Language : EN, JA, FR
Location : Macungie, PA

https://sethclydesdale.github.io/

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum