<!DOCTYPE>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Image extension validation in javascript</title>
<script type="text/javascript">
function checkExtension(filename)
{
var n= filename.lastIndexOf(".");
if(n != -1) //which means filename has .
{
var st= filename.substring(n);
if(st==".jpg" || st==".jpeg" || st==".png" || st==".gif")
{ //correct extension
return true;
}
else
{
//incorrect extension
return false;
}
}
else
{
//filname don't contains .
return false;
}
}
function check()
{
var fname=document.frm.filename.value;
if(fname=='')
{
alert('select image')
return false;
}
else if(checkExtension(fname))
return true;
else
{
alert('Invalid image file. Should be a image !!!')
return false;
}
}
</script>
</head>
<body>
<h1 align="center">Image extension validation in javascript.</h1>
<div align="center">
<form name="frm" onsubmit="return check()" method="post" action="go.html">
Select File:
<input type="file" name="filename" id="filename" />
<br/> <br/>
<input type="submit" />
</form>
</div>
</body>
</html>
Friday, December 12, 2014
How to Validate Image Extension in JavaScript ?
Labels:
javascript
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment