User2103319870 posted
You can use the below regular expression to validate the file extension.
^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.csv|.CSV)$
Complete Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function validate() {
var uploadcontrol = document.getElementById('<%=FileUpload1.ClientID%>').value;
//Regular Expression for fileupload control.
var reg = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.csv|.CSV)$/;
if (uploadcontrol.length > 0) {
//Checks with the control value.
if (reg.test(uploadcontrol)) {
return true;
}
else {
//If the condition not satisfied shows error message.
alert("Only .csv files are allowed!");
return false;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return validate();" />
</div>
</form>
</body>
</html>
Source URL