Click here to Skip to main content
11,705,212 members (53,262 online)
Click here to Skip to main content

Tagged as

Regular Expression to Validate File Path and Extension

, 28 May 2012 CPOL 70K 12
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

Introduction

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]\:|\\)(\\[a-z_\-\s0-9\.]+)+\.(txt|gif|pdf|doc|docx|xls|xlsx)$

Explanation

^(?:[\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)

Matches

\\192.168.0.1\folder\file.pdf
\\192.168.0.1\my folder\folder.2\file.gif
c:\my folder\abc abc.docx
c:\my-folder\another_folder\abc.v2.docx

Non-Matches

\\192.168.0.1\folder\fi<le.pdf
\\192.168.0.1\folder\\file.pdf
\\192.168.0.1\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
c:\my?folder\another_folder\abc.v2.docx
file.xls

Test It

License

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

Share

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.

You may also be interested in...

Comments and Discussions

 
SuggestionFile extensions order Pin
Sergei Vedischev19-Jun-14 6:06
memberSergei Vedischev19-Jun-14 6:06 
GeneralRe: File extensions order Pin
Bryian Tan30-Jul-14 4:36
memberBryian Tan30-Jul-14 4:36 
QuestionMore complete version Pin
DeepBlackHole11-Mar-13 17:45
memberDeepBlackHole11-Mar-13 17:45 
GeneralRe: More complete version Pin
Bryian Tan12-Mar-13 12:57
memberBryian Tan12-Mar-13 12:57 
QuestionHi Pin
Rogério F Moreira10-Aug-12 5:10
memberRogério F Moreira10-Aug-12 5:10 
AnswerRe: Hi Pin
Bryian Tan10-Aug-12 16:53
memberBryian Tan10-Aug-12 16:53 
QuestionMac and Linux? Pin
Menelaos Vergis28-May-12 23:15
memberMenelaos Vergis28-May-12 23:15 
GeneralRe: Mac and Linux? Pin
Bryian Tan29-May-12 5:21
memberBryian Tan29-May-12 5:21 
Questionhi Pin
beleshi28-May-12 21:28
memberbeleshi28-May-12 21:28 
SuggestionDid not work!! Pin
klnreddy26-Mar-12 8:27
memberklnreddy26-Mar-12 8:27 
GeneralRe: Did not work!! Pin
Bryian Tan26-Mar-12 16:21
memberBryian Tan26-Mar-12 16:21 
GeneralRe: Did not work!! Pin
gchq26-May-12 8:30
membergchq26-May-12 8:30 
GeneralRe: Did not work!! Pin
Bryian Tan28-May-12 16:40
memberBryian Tan28-May-12 16: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.150819.1 | Last Updated 28 May 2012
Article Copyright 2011 by Bryian Tan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid