One Javascript method for multiple buttons [Resolved]

Posted by Nerdanalysis under JavaScript on 4/17/2012 | Points: 10 | Views : 3446 | Status : [Member] | Replies : 3
Hi ,

I want to upload 4 images via 4 uploadfile asp control . I have a javascript function to check the validation of file extension. I want to call this function for every button click for the image upload. Please suggest me how to call the same function for different buttons. the code is :-

Javascript code :-

<script language="javascript" type="text/javascript">
function validate() {
var result = false;
var upfile = document.getElementById('fuMain').value;
if (upfile != "") {
var accept = "png,gif,jpg,jpeg".split(',');
var getExtention = upfile.split('.');
var extention = getExtention[getExtention.length - 1];
for (i = 0; i < accept.length; i++) {
if (accept[i] == extention) {
result = true;
break;
}
}
if (!result) {
alert("allowed file extetions are png,gif,jpg,jpeg");
}

}
else {
alert("select image to Upload");
}
return result;
}
</script>

the button and fileuploader code is

<td>
Main Image
</td>
<td>
<asp:FileUpload ID="fuMain" runat="server" />
</td>
<td>
<asp:Button ID="btnMainImage" runat="server" OnClick="btnMainImage_Click" OnClientClick="return validate();"
Text="Submit" />
</td>
</tr>
<tr>
<td>
Sub Image1
</td>
<td>
<asp:FileUpload ID="fuSub1" runat="server" />
</td>
<td>
<asp:Button ID="btnSubImage1" runat="server" Text="Submit" />
</td>
</tr>
<tr>
<td>
Sub Image2
</td>
<td>
<asp:FileUpload ID="fuSub2" runat="server" />
</td>
<td>
<asp:Button ID="btnSubImage2" runat="server" Text="Submit" />
</td>
</tr>
<tr>
<td>
Sub Image3
</td>
<td>
<asp:FileUpload ID="fuSub3" runat="server" />
</td>
<td>
<asp:Button ID="btnSubImage3" runat="server" Text="Submit" />
</td>
</tr>
<tr>
<td>
Sub Image4
</td>
<td>
<asp:FileUpload ID="fuSub4" runat="server" />
</td>
<td>
<asp:Button ID="btnSubImage4" runat="server" Text="Submit" />
</td>
</tr>




Responses

Posted by: dhirenkaunar-15094 on: 4/17/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi Narendra,

Please find the below code which will resolve your issue .Please mark as answer if it satisfy your answer.

I did the fallowing changes ..
Javascript code :-

//validate method with parameter : this is contains the fileTag value for the corresponding button.
Please fallow the bold onces
<script language="javascript" type="text/javascript">
function validate(fileObject) {
alert(fileObject);
var result = false;
var upfile = document.getElementById(fileObject).value;
if (upfile != "") {
var accept = "png,gif,jpg,jpeg".split(',');
var getExtention = upfile.split('.');
var extention = getExtention[getExtention.length - 1];
for (i = 0; i < accept.length; i++) {
if (accept == extention) {
result = true;
break;
}
}
if (!result) {
alert("allowed file extetions are png,gif,jpg,jpeg");
}

}
else {
alert("select image to Upload");
}
return result;
}
</script>
the button and fileuploader code is
<td>
Main Image
</td>
<td>
<asp:FileUpload ID="fuMain" runat="server" />
</td>
<td>
<asp:Button ID="btnMainImage" runat="server" OnClick="btnMainImage_Click" OnClientClick="return validate('fuMain');"
Text="Submit" />
</td>
</tr>
<tr>
<td>
Sub Image1
</td>
<td>
<asp:FileUpload ID="fuSub1" runat="server" />
</td>
<td>
<asp:Button ID="btnSubImage1" runat="server" Text="Submit"
OnClientClick="return validate('fuSub1');" onclick="btnSubImage1_Click"/>
</td>
</tr>
<tr>
<td>
Sub Image2
</td>
<td>
<asp:FileUpload ID="fuSub2" runat="server" />
</td>
<td>
<asp:Button ID="btnSubImage2" runat="server" Text="Submit"
OnClientClick="return validate('fuSub2');" onclick="btnSubImage2_Click"/>
</td>
</tr>
<tr>
<td>
Sub Image3
</td>
<td>
<asp:FileUpload ID="fuSub3" runat="server" />
</td>
<td>
<asp:Button ID="btnSubImage3" runat="server" Text="Submit"
OnClientClick="return validate('fuSub3');" onclick="btnSubImage3_Click"/>
</td>
</tr>
<tr>
<td>
Sub Image4
</td>
<td>
<asp:FileUpload ID="fuSub4" runat="server" />
</td>
<td>
<asp:Button ID="btnSubImage4" runat="server" Text="Submit"
OnClientClick="return validate('fuSub4');" onclick="btnSubImage4_Click"/>
</td>
</tr>

Thanks & Rgards,
Dhiren Kumar Kaunar

Nerdanalysis, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nerdanalysis on: 4/17/2012 [Member] Starter | Points: 25

Up
0
Down
Thanks a Million Dhiren ...it works...you are super....

Nerdanalysis, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: dhirenkaunar-15094 on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Welcome Narendra,



Thanks & Rgards,
Dhiren Kumar Kaunar

Nerdanalysis, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response