0

i have 3 input file, i wanna make a javascript validation for 3 input file in one submit button form event onSubmit=""

<form action="step2_crud_dev.php" method="post" enctype="multipart/form-data" class="form-horizontal" role="form" id="dataPribadi"  >
    <div>
    <input type="file" name="fUpload1" id="fileUpload1"/>
    <input type="file" name="fUpload2" id="fileUpload2"/>
    <input type="file" name="fUpload3" id="fileUpload3"/>
    </div>
    <div>
    <input type="submit" name="upload" value="upload" />
    </div>
</form>

EDITED CODE new code that working but, still proccess saving

$(document).ready(function(){
    $('#tbl_next').click(function(){
        //alert('hello');
        $('input[type="file"]').each(function(){
            var thisFile = $(this);
            var fileSize = thisFile[0].files[0].size;
            var fileType = thisFile[0].files[0].type;
            //alert(fileSize);

            if(fileSize>1048576){ //do something if file size more than 1 mb (1048576)
                alert(fileSize +" bites\n ukuran gambar terlalu besar");
                return false;
            }else{
                switch(fileType){
                    case 'image/png':
                    case 'image/gif':
                    case 'image/jpeg':
                    case 'image/pjpeg':
                        alert("Acceptable image file!");
                        break;
                    default:
                        alert('Unsupported File!');
                        return false;
                }
            }
        });
        $('form#dataPribadi').submit();
    });
});
7
  • 1
    And what have you tried so far? Commented Sep 11, 2014 at 6:18
  • 1
    IDs must be unique in HTML, Guys its unique identifier Commented Sep 11, 2014 at 6:18
  • wrap it up in a form element and then have a onSubmit function on it Commented Sep 11, 2014 at 6:21
  • i tried but it for by one validation or per file input at event onchange Commented Sep 11, 2014 at 6:23
  • @user3335420 have you tried the solution provided below? Commented Sep 11, 2014 at 7:02

1 Answer 1

1

Change type of submit button to normal button and use onclick handler for it by giving a id as upload,

<form action="step2_crud_dev.php" method="post" enctype="multipart/form-data" class="form-horizontal" role="form" id="dataPribadi"  >
    <div>
        <input type="file" name="fUpload1" id="fileUpload1"/>
        <input type="file" name="fUpload2" id="fileUpload2"/>
        <input type="file" name="fUpload3" id="fileUpload3"/>
    </div>
    <div>
        <input type="button" id="upload" value="upload" />
    </div>
</form>

and your click event handler should be,

$(document).ready(function(){
    $('#upload').click(function(){
        alert('hello');
        $('input[type="file"]').each(function(){
            var thisFile = $(this);
            var fileSize = thisFile[0].files[0].size;
            alert(fileSize);
        });
        $('form#dataPribadi').submit();
    });
});

UPDATED FIDDLE

Sign up to request clarification or add additional context in comments.

5 Comments

thanks it works for the function, i'll improve again
but i still need a lil help later, may be :D
Ohh, you can add to your question though. We are glad to help!
i already update the code (javascript) with new code that u give to me, but when its get false input (file too big) php code / saving proccess still go on. i already put "return false;" but its not working to stop the proccess.
it still go on, maybe because it each proses

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.