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.
The forum of the forums

    Block numbers in usernames at registration

    Ange Tuteur
    Ange Tuteur
    Forumaster


    Male Posts : 13207
    Reputation : 3000
    Language : English & 日本語
    Location : Pennsylvania

    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.

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


    SarkZKalie likes this post


      Current date/time is September 22nd 2024, 2:33 pm