2

I'm working on asp.net page wich contain a Multiview control and some wizard steps. I've got ten couple of textboxes named like TextboxA[i] and TextBoxB[i] and a counter. Foreach couple of not empty textboxes I want my counter++.

On server side I would be able to write in TextboxA[i] and TextboxB[i] onTextChange event something like

if(String.IsNullOrWhiteSpace(TextboxA[i]))
    counter++;

Not so good... I've to increment counter on client side using javascript but - if I have - I don't know how. Please, can anyone help me?

Amiram Korach
  • 13,056
  • 3
  • 28
  • 30
Barbara
  • 87
  • 1
  • 7
  • What have you tried so far? This is a basic javascript, easily searchable using Google. Do you have a specific problem with the implementation? It works somewhat similar to the C# equivalent, just the syntax differs... – walther Aug 20 '12 at 08:50

2 Answers2

0

First u need to give Id's to the text boxes. Like

TextboxA[i].ID = "textboxA" + i;
TextboxB[i].ID = "textboxB" + i;

then access those textboxes from client side using

var TextboxA = document.getElementById("textboxA" + i);
var TextboxB = document.getElementById("textboxB" + i);
if((TextboxA.value == null || TextboxA.value=="" )&&(TextboxB.value == null || TextboxB.value=="" ))
counter++;
Rajneesh
  • 2,185
  • 4
  • 20
  • 30
0

Client side code:

<script type="text/javascript">
var counter = 0;

function increaseCounter(textAID, textBID)
{
    var textA = document.getElementById(textAID);
    var textB = document.getElementById(textBID);
    if (isNullOrWhitespace(textA.value) && isNullOrWhitespace(textB.value))
        counter++;
}

function isNullOrWhitespace( input ) {

    if (input == null) return true;

    return input.replace(/\s/g, '').length < 1;
}
</script>

Code for isNullOrWhitespace taken from https://stackoverflow.com/a/5559461/1495902.

Server side code:

for (int i = 0; i < TextboxA.Count() ; i++)
{
    var script = "increaseCounter(" + TextboxA[i].ClientID + "," + TextboxB[i].ClientID + ")";
    TextboxA[i].Attributes.Add("onchange", script);
    TextboxB[i].Attributes.Add("onchange", script);
}
Community
  • 1
  • 1
Amiram Korach
  • 13,056
  • 3
  • 28
  • 30