Here Mudassar Ahmed Khan has explained with an example, how to allow only AlphaNumeric (Letters or Numbers), Underscore in Username and it must have length of 3 to 10 characters using JavaScript.
When the Submit Button is clicked, the Username in the TextBox will be validated using JavaScript and Regular Expression (Regex) and if the Username is invalid, the error message will be displayed next to the TextBox using JavaScript.