vandan's avatar
Level 13

validation txt,doc,pdf file using javascript

i use validation using javascript but when i upload text file then validation error how to check extesion in javascript

here is my script

<script>
        $('button[type="submit"]').prop("disabled", true);
        var a=0;        
        $('#idproof').bind('change', function() {
        if ($('button:submit').attr('disabled',false)){
                $('button:submit').attr('disabled',true);
        }
        var ext = $('#idproof').val().split('.').pop().toLowerCase();
        if ($.inArray(ext, ['gif','png','jpg','jpeg','text','rtf','doc','pdf']) == -1){
                $('#error1').slideDown("slow");
            a=0;
        }else{
                $('#error1').slideUp("slow");
                if (a==1){
                    $('button:submit').attr('disabled',false);
                    }
            }
        });
</script>

here is my blade file

<input type="file" name="idproof" id="idproof">
    <p id="error1" style="display:none; color:#FF0000;">
Invalid Image Format! Image Format Must Be JPG, JPEG, PNG, DOC ,TXT, PDF.
0 likes
11 replies
vandan's avatar
Level 13

@ahkeravi yes i need to upload text file doc file and pdf so how to validate

Sti3bas's avatar

@vandan I guess you have a typo in your extensions list, it should be txt instead of text.

['gif', 'png', 'jpg', 'jpeg', 'txt', 'rtf', 'doc', 'pdf']
vandan's avatar
Level 13

@sti3bas yes i changed txt but same error

i used ubuntu so text file is diffrent?

Sti3bas's avatar

@vandan $.inArray(ext, ['gif','png','jpg','jpeg','txt','rtf','doc','pdf']) == -1 will return true if extension is not found in the given list. And as far as I understand $('#error1').slideDown("slow"); should show the error. Seems like your condition is wrong, it should be $.inArray(ext, ['gif','png','jpg','jpeg','txt','rtf','doc','pdf']) != -1 so that the error would only be shown when extension is not found in the list.

1 like
ahkeravi's avatar

@vandan you can remove whichever file you want to upload

(['gif','png','jpg','jpeg','rtf','pdf'])

remove text and doc from array it will work

1 like
vandan's avatar
Level 13

@sti3bas no sir i want check if only text/pdf/jpg like file are store so but i use extension of text file 'txt' then error validation wrong so how to check in can ubuntu text file extension is diffrent?

vandan's avatar
Level 13

@ahkeravi no sir i want all file like pdf text all above extesnion are use and validate so and i check in condition txt in not support may be

Sti3bas's avatar

@vandan well, you can console.log(ext); to check the extension of the file you're trying to upload and add it to the list.

1 like
vandan's avatar
Level 13

@sti3bas yes i try display file like c:\fakepath\git step

so not extension show

ahkeravi's avatar

then you can use jquery for that u can easily achive this

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/additional-methods.js"></script>

<form id="add" enctype="multipart/form-data">
    <label>Song:</label>
    <input type="text" name="song" class="form-control">
    <br>
    <label>Upload Mp3:</label>
    <input type="file" name="file" class="form-control">
    <br>
    <input type="submit" class="btn btn-default" value="Add">
</form>
<script>
$("#add").validate({
    rules: {
        song: {
            required: true,
        },
        file: {
            required: true,
            extension: "mp3|mpeg|mp4"
        },
    }
});
<script>

@vandan

1 like

Please or to participate in this conversation.