The forum of the forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Block numbers in usernames at registration

Go down

Tutorial Block numbers in usernames at registration

Post by Ange Tuteur 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.

usernames - 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 : 13246
Reputation : 3000
Language : English & 日本語
Location : Pennsylvania

https://fmdesign.forumotion.com

SarkZKalie likes this post

Back to top Go down

Back to top

- Similar topics

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