0

Hello everyone I'm trying to put a file upload button and this button provides to upload video files with extension mp4,avi,swf so how can I check these multiple extension with javascript?

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <br />
        <p><input type="file" id="file" name="file" size="23"/></p><br />
        <p><input type="submit" value="Upload file" /></p>        
    }  

Javascript:

$(function () {
    $('form').submit(function () {
        var selectedFile = $('#file').val();
        var matches = selectedFile.match(/\.(xlsx?)$/i);
        if (matches == null) {
            alert('please select an Excel file');
            return false;
        }
        return true;
    });
});

1 Answer 1

1

Here I have some of regular expressions to validate file upload.

Regular expression to validate file formats for .mp3 or .MP3 or .mpeg or .MPEG or m3u or M3U

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

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

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

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

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

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

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

Using this you can change based upon your requirements...

 var myRegExp = /[a-zA-z]$/;
    if(myRegExp.test("testing the string"))
    {
      // Success
    }
    else
    {
      // Fail
    }

hope this helps :D ....

2
  • Yes it is usefull reqular expression but I need with javascript. Not ask me why you have to use javascript because of my client wants :))
    – learnmore
    Nov 2, 2012 at 14:35
  • You can refer the answer I Changed :D
    – Raghurocks
    Nov 2, 2012 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.