Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate a string to be URL safe using regex

I have a site where users can pick a username. Currently, they can put in almost any characters including things such as @ ! # etc.

I know I can use a regex, and that's probably what I'm opting for.

I'll be using a negated set, which I'm assuming is the right tool here as so:

[^@!#]

So, how can I know all of the illegal characters to put in that set? I can start manually putting in the ones that are obvious such as !@#$%^&*(), but is there an easy way to do this without manually putting every single one of them in?

I know a lot of sites only allow strings that contain alphabets, numbers, dashes, or underscores. Something like that would work well for me.

Any help would be greatly appreciated.

Thanks S.O.!

like image 943
Isaiah Lee Avatar asked Jun 25 '14 21:06

Isaiah Lee


1 Answers

Instead of denying values, maybe it's better to only allow some

[:word:] -- Digits, letters and underscore

Check this chart

http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

like image 87
brunofitas Avatar answered Oct 22 '22 18:10

brunofitas