Block numbers in usernames at registration

Go down

Block numbers in usernames at registration

Post by Ange Tuteur on July 21st 2014, 10:40 am

Block numbers in usernames at registration


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



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:
/^[^!@#$%^&*:;"',.?]*$/


avatar
Ange Tuteur
Forumaster

Male Posts : 13202
Reputation : 2917
Language : EN10, FR5, JA5
Location : Macungie, PA

https://github.com/SethClydesdale

Back to top Go down

Back to top


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