Even though you can easily bypass JavaScript validation by disabling JavaScript in the browser, you can prevent genuine user errors by implementing this code.
<html>
<head>
<title>Upload Check</title>
<script type="text/javascript">
function vali_type()
{
var id_value = document.getElementById('up').value;
if(id_value != '')
{
var valid_extensions = /(.jpg|.jpeg|.gif)$/i;
if(valid_extensions.test(id_value))
{
alert('OK');
}
else
{
alert('Invalid File')
}
}
}
</script>
</head>
<body>
<input type="file" name="up" id="up"
onBlur="vali_type()" />
</body>
</html>
var valid_extensions = /(.jpg|.jpeg|.gif)$/i;
Check for .jpg, .jpeg and .gif file extentions in the end of the string
| - OR
i - Case Insensitive
$ - END
thanks you
ReplyDeleteThanks for sharing, it came in very handy for a file upload I was doing.
ReplyDeleteThanks, it made my job very easy..:)
ReplyDeleteThank you so much...
Thanks, although I wonder why you have .test in this line?
ReplyDeleteif(valid_extensions.test(id_value))
where does it come from?
.test() is the method that used to do the regex validation.
ReplyDeletehttp://www.w3schools.com/jsref/jsref_regexp_test.asp
Thanks for sharing this information.
ReplyDeleteNice post
ReplyDeleteHey what if there is a table instead of a text box and if we had to get the value from the columns how can we do it?
ReplyDeletenice sir
ReplyDeleteGood One
ReplyDeleteHey there, this helped me with a similar problem, only 1 comment though, it should be /(\.jpg|\.jpeg|\.gif)$/i else it will return true if the filename contains jpg in the name, even if the extension is pdf or whatever :)
ReplyDeletenice post. many thanks
ReplyDeletethanks mate...nice one
ReplyDelete$/i wats the purpose
ReplyDeletecase-insensitive search
Delete