1

I want to extract a username from a string that user has typed into input. The thing is that I don't want just simply validate username, but I want to show for user what will be the final username even if user types any non-allowed characters.

So for example if user types in something like &%$User)(Nam-e it will show usernam-e

there is similar question with answer Regular expression to validate username, but somehow it gives me an error of Invalid group on node.js when I try to use it with a match or exec functions.

Anyway, most of the examples online only validates the username against regex, but not actually provides the outcome of the appropriate username.

Rules are following:

  1. Only contains alphanumeric characters, underscore, dash and dot.
  2. Underscore, dash and dot can't be at the end or start of a username (e.g _username / username_).
  3. Underscore, dash and dot can't be next to each other (e.g user_-.name).
  4. Underscore, dash or dot can't be used multiple times in a row (e.g user__name).

So far I was only capable to do something similar with using replace function number of times

value.replace(/[^\w-]*/g,'').replace(/^[^a-z]*/,'').replace(/-{2,}/g,'-').replace(/_{2,}/g,'_');

But this doesn't look like an efficient code, especially that I would actually need to add even more replace functions to extract appropriate username.

Any ideas how to achieve that?

1 Answer 1

1

Assumes that you want the name displayed in lower-case, as in your example:

function user( n ) {
    var name = n.replace( /^[^a-z]+|[^a-z\d_\-.]|[_\-.](?![a-z\d])/gi, '' );    
    if ( n != name ) {
        console.log( 'Username invalid' );
    }
    return name.toLowerCase(); 
}

user('&%$User)(Nam-e');    // Username invalid, usernam-e                                             
user('_Giedrius_one_');    // Username invalid, giedrius_one                                                                                                 
user('Giedrius--one');     // Username invalid, giedrius-one                                            
user('Giedrius-one');      // giedrius-one  
user('/.bob_/');           // Username invalid, bob                            
3
  • 1
    Found one mistake, if you type more than one of the allowed special character at the beginning, string will start with special character. .-username => -username . A slightly updated version of regex - /[^a-z\d_\-.]|^[_\-.]+|[_\-.](?=[_\-.])|[_\-.]$/gi
    – Giedrius
    Jan 21, 2013 at 0:03
  • After playing around found more mistakes, like \.bob_\ returning .bob_ Turns out that order is quite important. This one is the final one that I came up with and found no mistakes yet /^[^a-z]+|[^\w-.]|[_\-.](?=[^a-z0-9])|[^a-z\d]$/gi Thanks a lot for showing the right path.
    – Giedrius
    Jan 21, 2013 at 0:38
  • @Giedrius. Realised only one regex required and updated answer. Apologies for not testing more - it was late.
    – MikeM
    Jan 21, 2013 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.