lundi 28 décembre 2020

if statement to check file size AND file extension

I've got a basic if statement to check a file input's size and extension. Both my statements work separately, but I can't get both of them to work together.

To validate the extension:

//check file extension

let filename = imgInput.value
let last_dot = filename.lastIndexOf('.')
let ext = filename.slice(last_dot + 1)


if(ext !== 'jpg' || ext !== 'png' ){
    imgClient.innerHTML = 'Please select 1 JPG or PNG file less than 5mb'
    e.preventDefault()
} else {
    imgClient.innerHTML = ''
}

To validate the size:

// validate file size

if(imgInput.files[0]){
    let imgSize = imgInput.files[0].size
    if(imgSize > 5087408 ){
        imgClient.innerHTML = 'Your file is too big, please select an image under 5mb'
        e.preventDefault()
    } else {
        imgClient.innerHTML = ''
    }
}

One works when I comment the other out, but when I try both of them, only the first one executes.

I'm sure this might be a simple fix, I'm just stuck trying to figure it out... thanks for looking!

Aucun commentaire:

Enregistrer un commentaire