The forum of the forums
Welcome to the Official Support Forum of Forumotion!

To take full advantage of everything offered by our forum, please log in if you are already a member, or join our community if you've not yet.



Create a free forum like this one.

Block numbers in usernames at registration

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



Ange Tuteur
Forumaster

Male Posts : 13021
Reputation : 2683
Language : EN10, FR5
Location : Pennsylvania

http://fmdesign.forumotion.com

Back to top Go down

View previous topic View next topic Back to top


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