Hi guys i have a problem here

i have and input type file which will be containing an image file

<input type="file" onchange="validate(this)"/>

now my code below work in its extension detection but if it is invalid the input file still have i value
i want it that if the file is invalid the file that is being browse will not be the value
please see the javascript below:

var _validFileExtensions = ["jpg", "jpeg", "bmp", "gif", "png"];
function validate(data) {
    var isvalid = false;
    var photo = data.value.toLowerCase();
    photos = photo.split(".");
    photo = photos[photos.length-1];
    for(var x=0;x<_validFileExtensions.length;x++){
            if(_validFileExtensions[x]==photo){
                isvalid = true;
                break;
            }
    }
    if(!isvalid){
        alert('Invalid File');
        return false;
    }
}

Recommended Answers

All 4 Replies

I think data.value = null; should work, but it is quite possible that the file input is read-only. If so, the only option remaining is to remove the element entirely, and insert a new one into the DOM.

it still doesnt work input type still have a value...

remove the element entirely, and insert a new one into the DOM

yah i got your idea =) tnks anyway. You really a posting sage priteas =)..
i also thought of an idea in reseting the form =) like

document.formname.reset()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.