How can we identify textbox name in javascript?

java , javascript India
  • 17 years ago

    Hello All!,


    I am having 10 textboxes(html) and their names are name1,name2,name3....name10.
    I gave like following in java script,


    for(i = 1; i <=10; i++)
    {
       if (document.form6.name+i.value =="")
       {
          alert('Enter name'+i+'value');
          document.form6.name+i.focus();
        }
    }


    But it was not working. It gives NaN error. Please help me for this. What I did mistake here?
    It is urgent, please.
    Regards,
    Mohan

  • 17 years ago

    Try referencing the text boxes in an array named "name" first. You should be able to get at them with name[ i ] then.


    I suppose I'd better elaborate.


    Code:


    var name = new Array {document.form6.name1, document.form6.name2, document.form6.name3, ... }


    for(i = 0; i <=9; i++)
    {
       if (name[ i ].value =="")
       {
          alert('Enter name'+i+'value');
          name[ i ].focus();
        }
    }



    This should do the trick.

  • 17 years ago

    Thanks! It is working properly!
    But If it is more than 25 and above or dynamically if it increases, it is difficult to
    create array.


    Regards,
    mohan

  • 17 years ago

    If you want to dynamically adjust the number of text boxes when the page is loading, you could try to write the original script this way.


    Code:

    <script language="javascript">


    function dynamicTextboxes(no){


       var boxarray


       document.write("<script language='javascript'>");
       boxarray = "document.form6.name0" //We have to physically make the first entry ourselves
       for(i=1; i==no;i++) //create the list of textboxes
       {
            boxarray = boxarray + ",document.form6.name" + i
       }
       document.write("var name = new Array {" + boxarray + "}"); //declare the array of listboxes    
       /Now we just write the rest of the code as before/
       document.write("for (j=0; j==name.length; j++){");
       document.write("if (name [ i ].value==''){");
       document.write("alert('Enter name ' + j + ' value');");
       document.write("name [ j ].focus();}}");
    }//end function
    </script>



    I havn't tested this, but I'm guessing it should work.

  • 17 years ago

    The following code also works well thru eval() function.

    Code:

    function store1()
    {
       var i;
       for (i = 1; i <=document.form6.numrows.value; i++)
      {  
           if (eval("document.form6.name"+i+".value") == "")
          {
             alert("field should not be blank!");
             eval("document.form6.name"+i).focus();
             break;
                      }
      }
    }
       


    Regards,
    A.Mohan  

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Anyone who considers arithmetic methods of producing random digits is, of course, in a state of sin.” - John von Neumann