Link to home
Start Free TrialLog in
Avatar of Dmitriy
DmitriyFlag for United States of America

asked on

Is there a way to create a dynamic variable name in Java?

I have a webpage that is generated through a script. Let's say it has a column of text boxes with which is generated depending on the results returned from a search.  There are up to 100 text boxes that can be generated on the page.  I want to get each box name on the form and add it to a vector.

Naming convention is the following:  window.document.F1.t_ +the row number.  

Instead of doing
vector.addElement(window.document.F1.t_1);
vector.addElement(window.document.F1.t_2);
vector.addElement(window.document.F1.t_3);
...

I would like to do something like this:

for (int counter=1;counter<=10;counter++)
{  vector.addElement(window.document.F1.t_counter)
}

Is it possible to do something like this in java?
Avatar of allahabad
allahabad

Vector compContainer = new Vector();
for (int counter=1;counter<=100;counter++)   {
   compContainer.addElement("window.document.F1.t_"+String.valueOf(counter));
 }
Avatar of Dmitriy

ASKER

allahabad,

I am sorry I didn't make the question clearer.  I didn't mention that I would want to do something like this later:

for (i=0;i<vectorSize;i++)
{   if(vector.getElementAt(i).value = test)
       System.out.println("Test")

    else
       System.exit(0)
}

I will need to have an access to the values of the text boxes later.
You can acess those , once you have stored them in any container(in this case Vector).
Vector compContainer = new Vector();
              for (int counter=1;counter<=100;counter++)   {
                compContainer.addElement("window.document.F1.t_"+String.valueOf(counter));
              }

//Here textfield names are stored in Vector.
// Acess those from the vector
// do want this ?
for (int i=0 ; i<compContainer.size();i++) {
  System.out.println((String) compContainer.elementAt(i));
}
Avatar of Dmitriy

ASKER

Actually, I need to know what is inside those text boxes.  I am working with javascript and need to get the values in the textboxes,not their names, and verify that the values they have are all length of 8.
You asked,  I want to get each box name on the form and add it to a vector. , and your example also had name of text box. Is that not correct ?
Ok, all right.

Do you know values for  each text field, if you know  then you can store them  in the Hashtable as key value pair.  Key would be name of the field.

You can retreive those values from the methods available in Hashtable and check the length. Let me know , if you have problem.
if javascript, can use the follow:
for(var i=0;i<document.forms[0].elements.length;i++)
{
 if(document.forms[0].elements[i].type=="text")//can also add some condition such as name=="somename"
 {
  alert(document.forms[0].elements[i].value);
 }
}

if you want to check the value at server, you need submit the values to server, then at server get the values, use java check it.
ASKER CERTIFIED SOLUTION
Avatar of kokchoon78
kokchoon78

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This question belongs in the Javascript area, not Java:

but, this is easy.
When you dynamically generate the input elements, override "onBlur" to call a simple javascript method that verifies the element length. like   onBlur="checkLength(this);".  if the lenght check fails, return focus to 'this' element.

or loose the type=submit button for a type=button onClick="validateForm();" which calls document.formName.submit(); after validating lengths.