How to upload Only Excel file using RegularExpressionValidator?
i need to control Only ExcelFile uploading with RegularExpressionValidator How can i do that? i need to write a regex pattern in ValidationExpression...
<h3>FileUpload Test</h3>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button id="UploadBtn" Text="Upload File" OnClick="UploadBtn_Click"开发者_JAVA技巧 runat="server" Width="105px" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:RegularExpressionValidator
id="FileUpLoadValidator" runat="server"
ErrorMessage="Upload Excel only."
ValidationExpression="([a-z]\w*)(.xlsx|.xlsm|.xls)$"
ControlToValidate="FileUpload1">
</asp:RegularExpressionValidator>
</div>
I don't see why you'd validate the file name part - what's wrong about 123.abcd.xls
? Just use
^.*\.xls[xm]?$
Above given solutions might not work for extensions in upper cases. Enhancing "Penguen"'s solution:
Regex regx = new Regex(@"([a-zA-Z0_9].*\bxyz)|([a-zA-Z0_9].*\bXYZ)\b");
Replace "xyz" with xlsx and "XYZ" with "XLSX". Please give me alternate of this expression, if it is a bulky one.
var reg = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.xls|.xlsx|.XLS|.XLSX)$/;
if (reg.test($("[id$=FileUpload1]").val()))
{
return true;
}
ValidationExpression="[a-zA-Z0_9].*\bxlsx\b" is solution...
var ext = this.value.match(/\.(.+)$/)[1];
var cext = ext.toLowerCase();
switch (cext) {
case 'xlsx':
n code goes here. this checks if nly .xlsx is uploaded
精彩评论