Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

validate file size in js

function validateSize(input) {
  const fileSize = input.files[0].size / 1024 / 1024; // in MiB
  if (fileSize > 2) {
    alert('File size exceeds 2 MiB');
    // $(file).val(''); //for clearing with Jquery
  } else {
    // Proceed further
  }
}

<input onchange="validateSize(this)" type="file">
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #validate #file #size #js
ADD COMMENT
Topic
Name
5+8 =