本文介绍了使用Javascript进行文件类型验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对JavaScript验证有疑问。每当我的脚本运行时,我都会验证< input type =file>
,它会验证,但也会调用操作页面。我想停止操作页面,直到验证完成。这是我的代码,任何帮助都会很棒。问候
I have a question regarding to JavaScript validation. I am validaing the <input type="file">
whenever my scripts runs, it validates but also the action page is called. I want to stop the action page until the validation is complete. Here is my code, any help will be awesome. Regards
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Image Uploading</title>
</head>
<body>
<form name="xx" action="server.php" method="post" enctype="multipart/form-data" onsubmit="Checkfiles(this)">
<input type="file" name="file_uploading" id="filename">
<input type="submit" value="Submit" name="uploadfile">
</form>
<form name="view" method="post">
<a href="view_server.php">View your uploaded Images</a>
</form>
</body>
</html>
<script type="text/javascript">
function Checkfiles()
{
var fup = document.getElementById('filename');
var fileName = fup.value;
var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
if(ext =="GIF" || ext=="gif")
{
return true;
}
else
{
alert("Upload Gif Images only");
return false;
}
}
</script>
推荐答案
var fname = "the file name here.ext";
var re = /(\.jpg|\.jpeg|\.bmp|\.gif|\.png)$/i;
if(!re.exec(fname))
{
alert("File extension not supported!");
}
这篇关于使用Javascript进行文件类型验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!