Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  174] [ 4]  / answers: 1 / hits: 46814  / 15 Years ago, sun, january 10, 2010, 12:00:00

i have a form like this:



<form method=post src=upload enctype=multipart/form-data>
<input name=img1 id=img1 type=file>
<input type=submit value=Upload>
</form >


Please how can I valid this form using javascript so that only jpg files are uploaded. thanks for reading.


More From » file-upload

 Answers
7

You can bind the onsubmit event of your form, and check if the value of your file input ends with .jpg or .jpeg, something like this:



window.onload = function () {
var form = document.getElementById('uploadForm'),
imageInput = document.getElementById('img1');

form.onsubmit = function () {
var isValid = /.jpe?g$/i.test(imageInput.value);
if (!isValid) {
alert('Only jpg files allowed!');
}

return isValid;
};
};


Check the above example here.


[#97879] Wednesday, January 6, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jordenfabiand

Total Points: 678
Total Questions: 107
Total Answers: 95

Location: Western Sahara
Member since Mon, May 3, 2021
3 Years ago
;