Regular Expression for FileUpload in asp.net

Category: ASP.NETC#.NetVALIDATIONS Comments: No comments

Collection of Regular expression to validate various file formats :-

1.Regular expression to validate file formats for .mp3 or .MP3

Ex:

string str= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)$/;

2.Regular expression to validate file formats for .doc or .docx

Ex:

string str= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.DOC|.DOCX)$/;

3.Regular expression to validate file formats for .txt or .TXT

Ex:

string str= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.txt|.TXT)$/;

4.Regular expression to validate file formats for .jpeg or .JPEG or .gif or .GIF or .png or .PNG

Ex.

String str= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpeg|.JPEG|.gif|.GIF| .png|.PNG)$/;

5.By above all reg exp u can make your custom reg exp.

Ex:

String str= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpeg|.JPEG|.gif|.GIF| .png|.PNG)$/;

in place of .jpeg or .gif you can paste your custom file ext. like .zip or .rar etc.

now:- String str= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.”your_file_ext”)$/;

Leave a Reply

Your email address will not be published. Required fields are marked *

*

HTML tags are not allowed.