views:

98

answers:

2

want to get a validation script to validate the file upload box onsubmit.

check the extension of the file and validate accordingly to the extension of file loaded

+1  A: 

If you don't mind jquery, then you can use the validation plugin, which is available here. A brief introduction and small demo is available here.

Night Shade
sorry, most of my form <input> is done validated wih javascript..the only remaining part is the file upload box..no worries...thanks for you answers, I would rather try use it for my another module
Rahul TS
+1  A: 

Check here: http://jsfiddle.net/uh2Gn/

HTML:

<form method="post" enctype="" onsubmit="return validate()">
    <input type="file" id="file" />
    <input type="submit" />
</form>

JavaScript:

function validate() {
    var filename=document.getElementById('file').value;
    var extension=filename.substr(filename.lastIndexOf('.')+1).toLowerCase();
    //alert(extension);
    if(extension=='jpg' || extension=='gif') {
        return true;
    } else {
        alert('Not Allowed Extension!');
        return false;
    }
}

Keep in mind that this is only for user convenience, that he doesn't go theu a long submit process to get an error at the server, cuz for sure you must implement a check at server-side.

aularon
thanks for taking time o get me a good answer
Rahul TS
Welcome, any time : )
aularon