InRegex Logo
InRegex
New

Match Valid Username in Javascript

Explanation

This regex pattern matches a valid username that consists of alphanumeric characters (both uppercase and lowercase), underscores, with a length between 5 and 20 characters.

Javascript

Regular Expression

^[a-zA-Z0-9_]{5,20}$
Copy
Do you have Feedback?
Javascript

Example Usage

const regex = /^[a-zA-Z0-9_]{5,20}$/; 
const username = 'john_doe123'; 
if (regex.test(username)) { 
    console.log('Valid username'); 
} else { 
    console.log('Invalid username'); 
}
Copy

Test the expression

Similar Regular Expressions