Gayatri says:
<
asp:RegularExpressionValidator
validationexpression
=
"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.txt|.TXT|.PDF|.pdf|.DOC|.DOCX|.xlsx|.XLSX)$"
SetFocusOnError
=
"true"
ForeColor
=
"Red"
ControlToValidate
=
"FileUpload1"
Display
=
"Dynamic"
ID
=
"regexDoc"
runat
=
"server"
ErrorMessage
=
"Please select PDF, Excel,Docx or Text file."
></
asp:RegularExpressionValidator
><
br
/>
<
asp:RegularExpressionValidator
validationexpression
=
"/^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpeg|.JPEG|.gif|.GIF| .png|.PNG|.bmp|.BMP|.JPG|.jpg)$/"
SetFocusOnError
=
"true"
ForeColor
=
"Red"
ControlToValidate
=
"FileUpload1"
Display
=
"Dynamic"
ID
=
"regexImg"
runat
=
"server"
ErrorMessage
=
"Please select valid image."
></
asp:RegularExpressionValidator
>
1) You must be sure that your RegularExpression validator Validationexpression is correct.. For testing purpose i have changed the validationexpression to allow only Alphanumeric name files with correct extension as per types.
2) Also modified the script Code of function showMyImage and added of radio button click script to enable and disable the validator so it’s working fine as per RegularExpression validationexpression from Option selected.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=rdDocType] input[type=radio]").click(function () {
var docttype = $(this).val();
var regexDoc = document.getElementById("<%= regexDoc.ClientID %>");
var regexImg = document.getElementById("<%= regexImg.ClientID %>");
ValidatorEnable(regexDoc, false);
ValidatorEnable(regexImg, false);
if (docttype == "Document") {
$('#thumbs').html('');
ValidatorEnable(regexDoc, true);
ValidatorEnable(regexImg, false);
}
else {
ValidatorEnable(regexDoc, false);
ValidatorEnable(regexImg, true);
}
});
});
function showMyImage() {
var checkedRadio = $("#rdDocType input[type=radio]:checked");
if (checkedRadio.length > 0) {
if (checkedRadio.val() == "Document") {
$('#thumbs').html('');
}
else if (checkedRadio.val() == "Photo") {
var validFilesTypes = ["jpeg", "JPEG", "gif", "GIF", "png", "PNG", "bmp", "BMP", "JPG", "jpg"];
var file = $("[id*=FileUpload1]")[0];
var path = file.value;
var ext = path.substring(path.lastIndexOf(".") + 1, path.length).toLowerCase();
var isValidFile = false;
for (var i = 0; i < validFilesTypes.length; i++) {
if (ext == validFilesTypes[i]) {
isValidFile = true;
break;
}
}
if (isValidFile) {
var arrFiles = $('#<%=FileUpload1.ClientID %>').get(0).files.length
$('#thumbs').html('');
for (var i = 0; i < arrFiles; i++) {
var fr = new FileReader();
fr.onload = function (e) {
$('#thumbs').append('<img src="' + e.target.result + '" width="50px" height="50px" style="margin-right:5px;">')
}
fr.readAsDataURL($('#<%=FileUpload1.ClientID %>').get(0).files[i]);
}
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="col-md-12">
<div class="form-group">
<div class="col-sm-3">
<label class="pull-right">
Document Type<span style="color: red">*</span></label>
</div>
<div class="col-sm-6">
<asp:RadioButtonList ID="rdDocType" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="Document">Document</asp:ListItem>
<asp:ListItem Value="Photo">Photo</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ControlToValidate="rdDocType" ValidationGroup="MasterGroup"
ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required Field!!" SetFocusOnError="true"
ForeColor="Red"></asp:RequiredFieldValidator>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<div class="col-sm-3">
<label class="pull-right">
File Upload<span style="color: red">*</span></label>
</div>
<div class="col-sm-6">
<asp:FileUpload runat="server" ID="FileUpload1" Multiple="Multiple" onchange="showMyImage()" />
<asp:RequiredFieldValidator ControlToValidate="FileUpload1" ValidationGroup="MasterGroup"
ID="RequiredFieldValidator4" runat="server" ErrorMessage="Required Field!!" SetFocusOnError="true"
ForeColor="Red"></asp:RequiredFieldValidator><br />
<asp:RegularExpressionValidator ValidationExpression="([a-zA-Z0-9\s_\\.\-:])+(.doc|.docx|.txt|.TXT|.PDF|.pdf|.DOC|.DOCX|.xls|.XLS|.xlsx|.XLSX)$"
SetFocusOnError="true" ForeColor="Red" ControlToValidate="FileUpload1" ID="regexDoc"
runat="server" ErrorMessage="Please select PDF, Excel,Docx or Text file."></asp:RegularExpressionValidator><br />
<asp:RegularExpressionValidator ValidationExpression="([a-zA-Z0-9\s_\\.\-:])+(.jpeg|.JPEG|.gif|.GIF|.png|.PNG|.bmp|.BMP|.JPG|.jpg)$"
SetFocusOnError="true" ForeColor="Red" ControlToValidate="FileUpload1" ID="regexImg" runat="server" ErrorMessage="Please select valid image."></asp:RegularExpressionValidator>
<div id="thumbs">
</div>
</div>
</div>
</div>
</form>
</body>
</html>
Screenshot
