What would be a good regex to use for validating a username, forbidding all special chars except the "@" symbol in case people want to use their email address as their username?
2 Answers
Well I guess this may work for your circumstances (see below)...
var userRegex = /^[\w\.@]{6,100}$/;
This will match...
- word characters such as 0-9, A-Z, a-z, _
- literal period
- literal @
- between 6 and 100 characters long
You should probably look at the Email RFC, which states, amongst other characters, that the following are legal: ! # $ % & ' * + - / = ? ^ _ `` { | } ~. This means that the regex above will not allow all emails.
So...
var validUsername = document.getElementById('username').value.match(userRegex);