Regular expressions not working in google chrome

I want to filter on file uploader. It works fine in FireFox but in Google Chrome it always shows invalid file although file is valid .

function validate() {
    var uploadcontrol = document.getElementById('<%=fileupload.ClientID%>').value;

    //Regular Expression for fileupload control.
    //var reg = /^(([a-zA-Z])|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpg|.jpeg|.jpe|.gif|.bmp|.png|.JPG|.JPEG|.JPE|.GIF|.BMP|.PNG)$/;
    var reg = /^(([0-9a-zA-Z\^\&\'\@\{\}\[\]\,\$\=\!\-\#\(\)\%\+\~\_ ]))+(.jpg|.jpeg|.jpe|.gif|.bmp|.png|.JPG|.JPEG|.JPE|.GIF|.BMP|.PNG|.dds|.psd|.pspimage|.tga|.thm|.tif|.tiff|.yuv)$/;

    if (uploadcontrol.length > 0) {
        //Checks with the control value.
        if (reg.test(uploadcontrol)) {
            return true;
        }
        else {
            //If the condition not satisfied shows error message.
            alert("Only Images are allowed!");
            return false;
        }
    }
} //End of function validate.

Upvotes: 1

Views: 2100

Answers (2)

Ed_
Ed_

Reputation: 19098

Your test does seem to be working in chrome, see this fiddle.

As such I think you need to debug the value of uploadcontrol in chrome and firefox to see how they differ, then tune your regex.

Upvotes: 0

Tyron
Tyron

Reputation: 1976

That is a very strange regex you have there. Try this one

var reg = /^[^\\//]+\.(jpg|jpeg|jpe|gif|bmp|png|dds|psd|pspimage|tga|thm|tif|tiff|yuv)$/i;

This one works for me with various file names in chrome.

The i modifier in the end will ignore casing, and the name matching is a bit more generous like this.

Upvotes: 1

Related Questions