Liam
Liam

Reputation: 9855

jQuery, If file input extension is .pdf then run function

I'm trying to run a function if a file input type is pdf.

Does anybody know of a way i can validate the file extension with jQuery before hand? thanks

$('.uploadCV').click(function () {

    if ($('.html-btn').val() === '.pdf') {

        $('.join-us').flip({
            direction: 'rl',
            color: '#38404b',
            speed: 200,
            onAnimation: function () {
                $('.icon').fadeOut();
                $(this).css('margin-top', '20px');
            },
            content: '<span class="title">Uploading...</span><span class="summary">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>'
        })
    } else {
        console.log('dont upload');
    }
});

Upvotes: 0

Views: 4701

Answers (3)

Hamid Bahmanabady
Hamid Bahmanabady

Reputation: 665

In this plugin you can determine what extension can to upload Jquery Multiple File Upload Demo

Upvotes: 0

fiddle DEMO

You can check file extension .pdf

var val = $('.html-btn').val();
var file_type = val.substr(val.lastIndexOf('.')).toLowerCase();
if (file_type  === '.pdf') {

References

.substr()

.toLowerCase()

.lastIndexOf

Upvotes: 3

Adil
Adil

Reputation: 148140

You can check if string ends with .pdf

Live Demo

fileName = $('.html-btn').val().toLowerCase();
if(fileName.indexOf('.pdf') == fileName.length - 4) {

Upvotes: 1

Related Questions