2015-12-21 49 views
2

我的HTML看起来像这样:如何仅使用HTML5使用模式属性验证CSV文件上传?

<form> 
    <input type="file" name="txtFile" pattern="?" id="txtFile" class="required"/> 
</form> 

pattern = '?' 

使用它的正则表达式我想补充审定的唯一csv文件允许。

如果我上传.xls或任何其他文件,那么它将显示一个错误。

+0

你需要使用Javascript/jQuery代码来验证 –

+0

@Disha:是的,应该使用JS是可能的,但我一直在使用创建HTML5 .. –

+0

@Disha:阿含不错的编辑 – devpro

回答

7

现在你可以使用新的HTML5输入验证属性:

pattern="^.+\.(xlsx|xls|csv)$" 

接受的其他文件(参考:HTML5文档):类型

对于CSV:

<input type="file" accept=".csv" /> 

对于Excel文件,2003-2007(.xls):

<input type="file" accept="application/vnd.ms-excel" /> 

对于Excel文件,2010(.xlsx)格式:

<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /> 

对于文本文件(.txt):

<input type="file" accept="text/plain" /> 

对于图像文件(巴纽, .jpg等):

<input type="file" accept="image/*" /> 

对于HTML文件名(.htm或.html):

<input type="file" accept="text/html" /> 

对于视频文件(.AVI,.MPG,文件.mpeg,.MP4):

<input type="file" accept="video/*" /> 

对于音频文件(.MP3,.WAV等):

<input type="file" accept="audio/*" /> 

对于PDF文件,请使用:

<input type="file" accept=".pdf" /> 
+1

是的,它的工作!谢谢 –

+0

@Vijay:赞赏 – devpro

+0

http://stackoverflow.com/questions/11832930/html-input-file-accept-attribute-file-type-csv#11834872 ??? –

2

试试这个:

<input type="file" name="txtFile" accept=".csv" id="txtFile" class="required" /> 
+1

使用accept属性,您可以验证任何类型的文件扩展名,例如accept =“.csv,.xls,.xlsx” – aceraven777

+0

是!谢谢 –

4

使用此:

<input type="file" name="txtFile" id="txtFile" class="required" accept=".csv,text/csv" /> 

MIME类型是很好的做法。

+0

是的,也是它的工作!谢谢 –

相关问题