imperium2335
imperium2335

Reputation: 24112

Using regex to validate file

I am trying to validate image files before they are uploaded using javascript/jquery.

Here is what I have so far:

$('#cropimages').click(function(){
    i = 1 ;
    var valid = new RegExp('/^.*\.(jpg|jpeg|png|gif)$/') ;
    $('input').each(function(){

        if($(this).attr('name') == 'file'+i)
        {
            val = $(this).val() ;

            r = valid.exec(val) ;
            alert(r) ;
            i++ ;
        }
    })
    //$('#topperform').submit()
})

But it keeps coming back null no matter what kind of file I choose.

What do I need to do to my RegExp to make this work?

Upvotes: 0

Views: 1805

Answers (1)

Mark Byers
Mark Byers

Reputation: 837986

Use a regular expression literal:

var valid = /^.*\.(jpg|jpeg|png|gif)$/;

If you really want to use the RegExp consturctor then omit the delimiters.

var valid = new RegExp('^.*\.(jpg|jpeg|png|gif)$');

Related

Upvotes: 6

Related Questions