Click here to Skip to main content
11,410,118 members (62,149 online)
Click here to Skip to main content

Tagged as

Regular Expression to Validate File Path and Extension

, 28 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Regular Expression to validate the file path and extension and it is compatible with JavaScript and ASP.NET


Recently, I was looking for a regular expression to validate a file path and extension. I found several of them on Google, but none of them fit my requirement. So I decided to put together a version that suits my need. Here is the Regular Expression to validate the file path and extension and it is compatible with JavaScript and ASP.NET. I hope someone will find this information useful and that it will make your programming job easier.



^(?:[\w]\:|\\) -- Begin with x:\ or \\
[a-z_\-\s0-9\.] -- valid characters are a-z| 0-9|-|.|_ (you can add more)
(txt|gif|pdf|doc|docx|xls|xlsx) -- Valid extension (you can add more)


\\\my folder\folder.2\file.gif
c:\my folder\abc abc.docx


\\\my folder\folder.2\.gif
c:\my folder\another_folder\.docx
c:\my folder\\another_folder\abc.docx
c:\my folder\another_folder\ab*c.v2.docx

Test It


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bryian Tan
Software Developer
United States United States
I have over three years of experience working with Microsoft technologies. I have earned my Microsoft Certified Technology Specialist (MCTS) certification. I'm a highly motivated self-starter with an aptitude for learning new skills quickly.

Comments and Discussions

SuggestionFile extensions order PinmemberSergei Vedischev19-Jun-14 7:06 
GeneralRe: File extensions order PinmemberBryian Tan30-Jul-14 5:36 
QuestionMore complete version PinmemberDeepBlackHole11-Mar-13 18:45 
GeneralRe: More complete version PinmemberBryian Tan12-Mar-13 13:57 
QuestionHi PinmemberRogério F Moreira10-Aug-12 6:10 
AnswerRe: Hi PinmemberBryian Tan10-Aug-12 17:53 
QuestionMac and Linux? PinmemberMenelaos Vergis29-May-12 0:15 
GeneralRe: Mac and Linux? PinmemberBryian Tan29-May-12 6:21 
Questionhi Pinmemberbeleshi28-May-12 22:28 
SuggestionDid not work!! Pinmemberklnreddy26-Mar-12 9:27 
GeneralRe: Did not work!! PinmemberBryian Tan26-Mar-12 17:21 
GeneralRe: Did not work!! Pinmembergchq26-May-12 9:30 
GeneralRe: Did not work!! PinmemberBryian Tan28-May-12 17:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.5 | Last Updated 28 May 2012
Article Copyright 2011 by Bryian Tan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid