0

On my site I have multi file upload form with fields:

<input name="files[]" type="file" id="files" size="30" />
<input name="files[]" type="file" id="files" size="30" />
<input name="files[]" type="file" id="files" size="30" />

and I want to validate this fields with javascript code, I know how to get value for some simple fields with javascript but I don`t know how to get values from this fields with names "files[]", how javascript see this fields, array or...? How to apply validation of size and file type using javascript

3
  • Not quite get it, but I assume you want to select the DOM? Try change the id to class, since you cannot have multiple IDs, and use document.getElementsByClassName("files") to select the DOM. Commented Mar 8, 2017 at 12:39
  • for which type of files you want to put validation? Commented Mar 8, 2017 at 12:46
  • Hope it can help somebody stackoverflow.com/questions/64663160/… Commented Dec 22, 2020 at 16:32

4 Answers 4

0

<!DOCTYPE html>
<html>
<body>
<input name="files[]" type="file" id="files[]" size="30" />
<input name="files[]" type="file" id="files[]" size="30" />
<input name="files[]" type="file" id="files[]" size="30" />
<button onclick="myFunction()">Get File Name-Type-Size</button>
<script>
function myFunction() {
    var input, file;

    if (!window.FileReader) {
        bodyAppend("p", "The file API isn't supported on this browser yet.");
        return;
    }

    var input=document.getElementsByName('files[]');
	for(var i=0;i<input.length;i++){
		   var file = input[i].files[0];
			bodyAppend("p", "File " + file.name + " is " + formatBytes(file.size) + " in size"+" & type is "+ fileType(file.name));    
		}
	}

	//function to append result to view
	function bodyAppend(tagName, innerHTML) {
		var elm;

		elm = document.createElement(tagName);
		elm.innerHTML = innerHTML;
		document.body.appendChild(elm);
	}
	
	//function to find size of file in diff UNIT
	function formatBytes(bytes,decimals) {
	   if(bytes == 0) return '0 Byte';
	   var k = 1000;
	   var dm = decimals + 1 || 3;
	   var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
	   var i = Math.floor(Math.log(bytes) / Math.log(k));
	   return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
	}

	//function to find file type
	function fileType(filename){
		return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined;
	}
</script>

</body>
</html>

Check this now you can put conditions on type of file & size.

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

Comments

0
// Try this jQuery ;)
$("form").on('change', 'input[type=file]', function() {
    var file;
    var this = $(this);
    if (file = this.files[0])
    {
        var img = new Image();

        img.onload = function () {
            // correct 
            // check alert(img.src)
        }

        img.onerror = function () {
            //error info
        };

        img.src = _URL.createObjectURL(file);
    }
}    

Comments

0

<!DOCTYPE html>
<html>
<body>




<input name="files[]" type="file" id="files[]" size="30" />
<input name="files[]" type="file" id="files[]" size="30" />
<input name="files[]" type="file" id="files[]" size="30" />
<button onclick="myFunction()">Get File Type</button>
<script>

function myFunction() {
var file=document.getElementsByName('files[]');
for(var i=0;i<file.length;i++){
console.log(file[i].value);
}

}
</script>

</body>
</html>

you can get files name like this.

1 Comment

Thanks But how can apply image size validation and type validation
0

Thanks Bhavik vora But already done with this one

   <html>
    <head>
    <title>client-side image (type/size) upload validation</title>
    <meta charset=utf-8>
    <style>
    </style>
    </head>
    <body>
    <form><fieldset><legend>Image upload</legend>
    <input type="file"  name="file[]" onchange="getImg(this,100,'jpeg|png')">
    <input type="file"  name="file[]" onchange="getImg(this,100,'jpeg|png')">  
    </fieldset>
    </form>
    <script>
    function getImg(input,max,accepted){
        var upImg=new Image(),test,size,msg=input.form;
        msg=msg.elements[0].children[0];

        return input.files?validate():
        (upImg.src=input.value,upImg.onerror=upImg.onload=validate);
            "author: b.b. Troy III p.a.e";
        function validate(){
            test=(input.files?input.files[0]:upImg);
            size=(test.size||test.fileSize)/1024;
            mime=(test.type||test.mimeType);
        mime.match(RegExp(accepted,'i'))?
        size>max?(input.form.reset(),msg.innerHTML=max+"KB Exceeded!"):
        msg.innerHTML="Upload ready...":    
        (input.form.reset(),msg.innerHTML=accepted+" file type(s) only!")
        }
    }
    </script> 
    </body>
    </html>

Comments

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.