Hi,
I was referring muddasar's one of the article for the validation.
Below is my code and i am trying to validate the file upload selected file format on the submit button. I have 4 upload control and fu1 is mandatory field and others are not mandatory fields. Though i need to validate all the four uploads if the files have been selected. My full sample code is below for easy testing,
<asp:FileUpload runat="server" ID="fu1" />
<asp:Label ID="lblMessage1" runat="server"></asp:Label>
<asp:FileUpload runat="server" ID="fu2" />
<asp:Label ID="lblMessage2" runat="server"></asp:Label>
<asp:FileUpload runat="server" ID="fu3" />
<asp:Label ID="lblMessage3" runat="server"></asp:Label>
<asp:FileUpload runat="server" ID="fu4" />
<asp:Label ID="lblMessage4" runat="server"></asp:Label>
<asp:Button ID="btnSubmit" runat="server" ValidationGroup="vgSubmit"
Text = "Validaete" OnClick="btnSubmit_Click" OnClientClick="return ValidateUploads()" />
<script type="text/javascript">
var validFilesTypes = ["bmp", "gif", "png", "jpg"];
function ValidateUploads() {
return ValidateUpload1();
return ValidateUpload2();
return ValidateUpload3();
return ValidateUpload4();
}
function ValidateUpload1() {
var fu1 = document.getElementById("<%=fu1.ClientID%>");
var lblMessage1 = document.getElementById("<%=lblMessage1.ClientID%>");
var fu1path;
// Check Upload1 File format
var fu1path = fu1.value;
var isValidFile = false;
if (fu1path != null && fu1path != '') {
var ext = fu1path.substring(fu1path.lastIndexOf(".") + 1, fu1path.length).toLowerCase();
for (var i = 0; i < validFilesTypes.length; i++) {
if (ext == validFilesTypes[i]) {
isValidFile = true;
break;
}
}
if (!isValidFile) {
lblMessage1.style.color = "red";
lblMessage1.innerHTML = "Invalid File. Please upload a File with" +
" extension:\n\n" + validFilesTypes.join(", ");
}
}
else { isValidFile = false; }
return isValidFile;
}
function ValidateUpload2() {
var fu2 = document.getElementById("<%=fu2.ClientID%>");
var lblMessage2 = document.getElementById("<%=lblMessage2.ClientID%>");
var fu2path;
// Check Upload2 Image Type format
var fu2path = fu2.value;
alert(fu2path);
var isValidFile = false;
if (fu2path != null && fu2path != '') {
var ext = fu2path.substring(fu2path.lastIndexOf(".") + 1, fu2path.length).toLowerCase();
for (var i = 0; i < validFilesTypes.length; i++) {
if (ext == validFilesTypes[i]) {
isValidFile = true;
break;
}
}
if (!isValidFile) {
lblMessage2.style.color = "red";
lblMessage2.innerHTML = "Invalid File. Please upload a File with" +
" extension:\n\n" + validFilesTypes.join(", ");
}
}
else { isValidFile = true; }
return isValidFile;
}
function ValidateUpload3() {
var fu3 = document.getElementById("<%=fu3.ClientID%>");
var lblMessage3 = document.getElementById("<%=lblMessage3.ClientID%>");
var fu3path;
// Check Product Upload Image Type format
var fu3path = fu3.value;
alert(fu3path);
var isValidFile = false;
if (fu3path != null && fu3path != '') {
var ext = fu3path.substring(fu3path.lastIndexOf(".") + 1, fu3path.length).toLowerCase();
for (var i = 0; i < validFilesTypes.length; i++) {
if (ext == validFilesTypes[i]) {
isValidFile = true;
break;
}
}
if (!isValidFile) {
lblMessage3.style.color = "red";
lblMessage3.innerHTML = "Invalid File. Please upload a File with" +
" extension:\n\n" + validFilesTypes.join(", ");
}
else { isValidFile = true; }
}
return isValidFile;
}
function ValidateUpload4() {
var fu4 = document.getElementById("<%=fu4.ClientID%>");
var lblMessage4 = document.getElementById("<%=lblMessage4.ClientID%>");
var fu4path;
// Check Ingredients Upload File format
var fu4path = fu4.value;
alert(fu4path);
var isValidFile = false;
if (fu4path != null && fu4path != '') {
var ext = fu4path.substring(fu4path.lastIndexOf(".") + 1, fu4path.length).toLowerCase();
for (var i = 0; i < validFilesTypes.length; i++) {
if (ext == validFilesTypes[i]) {
isValidFile = true;
break;
}
}
if (!isValidFile) {
lblMessage4.style.color = "red";
lblMessage4.innerHTML = "Invalid File. Please upload a File with" +
" extension:\n\n" + validFilesTypes.join(", ");
}
else { isValidFile = true; }
}
return isValidFile;
}
</script>
I am doing slight mistake on the return to the main function. if i test indvidual function it works fine. whereas if i include the 4 functions in one function with the return statement, it's not validating. goes to to the server.
Any suggestions or corrrections please