Javascript Javascript在上传前验证文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8021880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript validate filename before upload
提问by pete
I simply want to validate the filename of the image being uploaded to ensure that it does not have spaces or unusual characters.
我只是想验证正在上传的图像的文件名,以确保它没有空格或异常字符。
This is my latest attempt from searching around, still no luck. Could it be something to do with the path of the file? is it taking this or just the file name into account?
这是我搜索的最新尝试,仍然没有运气。可能与文件路径有关吗?是考虑这个还是只考虑文件名?
I have this and a check of the extention working server side with php, but I would like a prompt to the user before submitting.
我有这个并用php检查扩展工作服务器端,但我想在提交之前提示用户。
At this point in time im getting the alert pop up even whether i use a file name it should accept or one that it should reject.
此时,即使我使用它应该接受的文件名或它应该拒绝的文件名,我也会弹出警报。
JavaScript
JavaScript
function validate(elem){
var alphaExp = /^[a-zA-Z_-]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert("File name not suitable");
elem.focus();
return false;
}
}
HTML
HTML
<label for="file">Filename:</label>
<input type="file" name="filename" id="filename" onchange="validate(this)" />
<p><input type="submit" name="submit" class="submit" value="Submit" />
</form>
回答by Leo
You will need to use a much more complex regular expression for this, because the elem.value
you are checking won't be something like image123.jpg
but more something like C:\fakepath\randomfolder\some other folder\image123.jpg
您将需要为此使用更复杂的正则表达式,因为elem.value
您正在检查的不是类似的东西,image123.jpg
而是更多类似的东西C:\fakepath\randomfolder\some other folder\image123.jpg
You might want to check into this : http://www.codeproject.com/Tips/216238/Regular-Expression-to-validate-file-path-and-exten
您可能想检查一下:http: //www.codeproject.com/Tips/216238/Regular-Expression-to-validate-file-path-and-exten
The exemple you'll find on this page is mostly for documents, not images, but you can twist it a bit to fit your needs like this :
您将在此页面上找到的示例主要用于文档,而不是图像,但您可以将其稍微扭曲以适应您的需求,如下所示:
^(?:[\w]\:|\)(\[a-z_\-\s0-9\.]+)+\.(png|gif|jpg|jpeg)$
回答by Enigma State
you can use this function too....
你也可以使用这个功能......
<script type="text/javascript">
function getNameFromPath(strFilepath) {
var objRE = new RegExp(/([^\/\]+)$/);
var strName = objRE.exec(strFilepath);
if (strName == null) {
return null;
}
else {
return strName[0];
}
}
</script>
and
和
<script language="javascript">
function Checkfiles() {
var fup = document.getElementById('filename');
var fileName = fup.value;
var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
if(ext == "gif" || ext == "GIF" || ext == "JPEG" || ext == "jpeg" || ext == "jpg" || ext == "JPG" || ext == "doc") {
return true;
} else {
alert("Upload Gif or Jpg images only");
fup.focus();
return false;
}
}
</script>
回答by pete
it is very very simple with test
function
test
功能非常简单
function validate(form){
if (/\s/.test(form.elements.file.value)) {
alert(' filename contains spaces. Please rename the file.');
return false;
}
return true;
}
<html>
<body>
<form onsubmit="return validate(this);">
<input type="file" name="file" value="" >
<input type="Submit" value="Submit" >
</form>
</body>
</html>