views:

32

answers:

1

I'm building a File manager with jQuery. Users can create folders and upload files. I need a Regual Expression (javascript) to check if the entered folder-name or the uploaded file-name is web-save. (no special chars or spaces are allowed)

The name should only contain alphanumeric values (a-z,A-Z,0-9), underscores (_) and dashes(-)

+2  A: 

This seems quite straightforward:

/^[\w.-]+$/

Useful tutorial

SilentGhost
/^[\w\-]+$/ : you need a backslash for the hyphen
Fabrizio Calderan
@Fabrizio: no you don't
SilentGhost
@Fabrizio: Not at the end of a capture group.
Felix Kling
oh I forgot one thing, dots(.) are allowed too offcourse for filenames
yens resmann
@yens: fixed that.
SilentGhost
@SilentGhost thanks, that's it!
yens resmann
Before the regex match it should be ensured that the filename is not equal with `.` and `..`.
splash
@splash thanks forgot about that!
yens resmann