choxnox
choxnox

Reputation: 134

Username validation with Regex

I have a signup form where user can enter, along other information, company's name and company's URL ID. Now, I'd like to suggest "URL ID" to the user so when he types "name", "URL ID" should be based on this input (very similar to Facebook "name" and "username" paradigms). I should also mention that user can manually type in "URL ID" for corrections after it was suggested.

It's important to mention here that based on this question I was able to successfully implement server-side validation for the "name" field using the following Regex expression (client-side validation is not needed):

/^[\p{L}\p{N}]+(?:[- \'\x26][\p{L}\p{N}]+| [\x26] [\p{L}\p{N}]+)*$/iu

There are certain rules that must be applied to URL IDs:

As an example of what's valid and what's not, here are some examples:

Valid inputs

Invalid inputs

To make the long story short, two things need to be done:

I guess I only need the valid Regex expression, I'm able to code the rest myself. Any help would be appreciated.

Upvotes: 0

Views: 2892

Answers (1)

Flavio Sousa
Flavio Sousa

Reputation: 455

I do not know if I understand all the rules, but according to his examples this regexp validates exactly what you need!

^[a-zA-Z0-9]+([-_\.][a-zA-Z0-9]+)*[a-zA-Z0-9]$

http://regexr.com?36f5b

Upvotes: 2

Related Questions