Reputation: 1848
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?
Upvotes: 1
Views: 9410
Reputation: 490233
Well I guess this may work for your circumstances (see below)...
var userRegex = /^[\w\.@]{6,100}$/;
This will match...
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);
Upvotes: 2
Reputation: 91270
^([a-zA-Z0-9.]+@){0,1}([a-zA-Z0-9.])+$
This will permit A-Z, a-z, 0-9 and ., and at most one @
Upvotes: 2