Click here to Skip to main content
15,911,848 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello People,

I need some help.I am using VS2008.

I have a repeater control which has 5 columns . First 4 columns contains 4 textboxes and last column is a button. Now i want to put a validation on 4 textboxes that whenever i click on the button, it should call javascript to check if the sum of 4 textbox should exactly be 100.

Now my prob is that in the javascript function, how can i "access the repeater textboxes" one by one so that i can total them and show an alert message accoringly..


Quick help would be appreciated.
Regards,

Shikhar
Posted

1 solution

Hello,

I have 2 simple solutions

1. using the usual Js function "getElementsByTagName" this will bring us all the input controls in the page , for a more accurate result you might need to filter them based on their type or maybe any other attribute , check the below

function ValidateMe() {
var AllTxtBoxes = new Array;

AllTxtBoxes = document.getElementsByTagName("input");
for (var i = 0; i <= AllTxtBoxes.length - 1; i++)
{if (AllTxtBoxes[i].type == "text")
//do your work
;
}
}

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<span id="label" runat="server"></span>
<input type="text" runat="server" onchange="ValidateMe();" />
</ItemTemplate>
</asp:Repeater>

this is in case these are the only text boxes in the page .

2. i think this solution is cleaner , using jQuery you can get them first by tag name , and then by having a specific attribute in this case you can add your own custom attribute and then read it in Js , check the below

your text box will be updated to
<input type="text" runat="server" onchange="ValidateMe();" MyCustomAttr="1" />

and in the ValidateMe function you will need one line of code
AllTxtBoxes = jQuery("input[MyCustomAttr]");


Hope this was of a value , good Luck ! :)
 
Share this answer
 
Comments
shikhar gilhotra 13-Feb-12 15:04pm    
and what if i have only need to add specific textboxes...??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900