如何在JavaScript中验证文件上传 [英] How to validate fileupload in javascript
本文介绍了如何在JavaScript中验证文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在JavaScript中对文件上传进行客户端验证?需要示例代码.
How to do client side validation of fileupload in javascript? Need Sample code.
推荐答案
^ ]
http: //stackstackflow.com/questions/2895099/how-can-we-can-we-apply-client-side-validation-on-fileupload-control-in-asp-net-to-check [ Google [
http://stackoverflow.com/questions/71944/how-do-i-validate-the-file-type-of-a-file-upload[^]
http://stackoverflow.com/questions/2895099/how-can-we-apply-client-side-validation-on-fileupload-control-in-asp-net-to-check[^]
Google[^] for more.
您可以使用正则表达式来获取文件扩展名,如下所示:
you can use regular expression to get file extension like this:
var extens=(/[.]/.exec(myfile)) ? /[^.]+
/.exec(myfile):未定义;
/.exec(myfile) : undefined;
myfile是文件的地址或路径.这只是将字符串放在."之后. .然后,您可以使用if进行一些控制检查.像:
myfile is the address or the path of the file. This simply takes the part of the string after "." . Then you can place some control checks with if. Something like :
if(extens=="doc" || extens=="docx"|| extens=="xls")
{
//your method goes here
}
else
{
alert("file is invalid");
}
您也可以参考此以获得更多帮助:
http://www.coursesweb.net/javascript/check-file-type-before-upload_t [^ ]
希望对您有所帮助:)
You can also refer to this for more help:
http://www.coursesweb.net/javascript/check-file-type-before-upload_t[^]
hope it helps :)
这篇关于如何在JavaScript中验证文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文