Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


regex for filename validation

regex for filename validation

regex for filename validation


I've been looking everywhere for a regex to validate a filename.  Obviously this would disallow:
\ / : * ? " < > |

Someone must have this in their arsenal.  Thanks in advance.

RE: regex for filename validation

You would also need to disallow certain specific filenames that can cause Windows boxes to hang / error, such as:

lpt1 (... lptX)


Hope this helps,

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch

RE: regex for filename validation

I'm not sure what the *exact* rules on filenames are anymore.  Windows filenames don't nearly have the restrictions that they used to.  Anyway, here's a regexp that disallows all the characters that you listed - additionally it will not allow multiple periods in a row (is that even valid?).  Additionally, it requires the filename to have at least one period in it - which I know is not necessary.  If you want to take that check out you can change the character marked in red to a *

This should cover most of your bases:


<script type="text/javascript">

function blah(str) {
   if (/^[^\\\/\:\*\?\"\<\>\|\.]+(\.[^\\\/\:\*\?\"\<\>\|\.]+)+$/.test(str)) {
      alert("valid file name");
   else {
      alert("this file name is invalid");


<input type="text" id="txt" />
<input type="button" value="click me" onclick="blah(document.getElementById('txt').value)" />


Looking for a puppy?

Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.

RE: regex for filename validation


Thanks a bunch kaht, a star for your efforts.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close