2015-12-21 52 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

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

相關問題