Javascript regex for validating filenames

javascript, regex

Solution

You need to add a starting anchor:

/^[0-9a-zA-Z ... ]+$/

This tells the engine to match from the start of the input all the way to the end of the input, whereas for your original expression it only needs to match at the end of the input.

Problem

I have a regexp to validate file names. Here is it: ``` /[0-9a-zA-Z\^\&\'\@\{\}\[\]\,\$\=\!\-\#\(\)\.\%\+\~\_ ]+$/ ``` It should allow file names like this: ``` aaa aaa.ext a# A9#.ext ``` The following characters are not allowed `\ / : * ? \" < > |` The problem is that file names like `*.txt` or `/\kk` passes the validation. I am doing validation with keyup event. So when I put one extra character after not allowed one it shows that everything is correct.

Original source