Javascript-HTML elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darksteel21
    New Member
    • Jul 2008
    • 36

    Javascript-HTML elements

    Hi to all,

    Im seeking for a solution for my problem in javascript..how can i get the values of an unspecified number of html textfields using javascript??

    The scenario is this:
    The user will specify the number of fields he wants, for example he specified 3 textfields,then my script will generate 3 html textfields..i want to validate these three textfields before submitting the form,it should not contain NULL values..how should i do this?? hope someone could help me...
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Write a generic validator that takes any input field and validates it according to your requirements. You then call that finction on each of your text boxes.

    Comment

    • darksteel21
      New Member
      • Jul 2008
      • 36

      #3
      what do you mean by generic validator?pleas e pardon me because im not that proficient in javascript...
      But I have a script here to get the values of Html textfields:
      [html]
      <html>
      <body>
      <script language="JavaS cript">
      function function1() {
      var m = document.getEle mentsByName("my E");
      alert(m.length) ;
      alert(m[0].name + "/" + m[0].value);
      alert(m[1].name + "/" + m[1].value);
      }
      </script>

      <form name="form1">
      <input type="text" name="mytxt1" id="myE" value="">
      <input type="text" name="mytxt2" id="myE" value="">
      <input type="button"
      value="Get the number of elements that have the same name"
      onClick="functi on1();">
      </form>
      </body>
      </html>
      [/html]
      it works fine on IE browsers..but not on firefox...
      when you browse it on IE it detects 2 textfields, but on Firefox it only detects nothing...can you please explain me this problem?
      Last edited by gits; Jul 25 '08, 09:42 AM. Reason: fixed bold-tags to code-tags

      Comment

      • rnd me
        Recognized Expert Contributor
        • Jun 2007
        • 427

        #4
        ids need to be different. names must be the same to use doc.getbyname

        Comment

        • RamananKalirajan
          Contributor
          • Mar 2008
          • 608

          #5
          Hi Dude, I hope this code will help you. But you have to enhance yourself to your need.

          [HTML]<html>
          <head>
          <script language="javas cript">
          var choice;
          function createText()
          {
          choice=parseInt (document.getEl ementById('user ').value);
          var textId;
          for(var i=1;i<=choice;i ++)
          {
          textId = "textId"+i;
          var udiv=document.g etElementById(' myDiv')
          var newEl=document. createElement(' input');
          newEl.type='tex t';
          newEl.id = textId;
          udiv.appendChil d(newEl);
          }
          }

          function validateText()
          {
          var textId
          for(var i=1;i<=choice;i ++)
          {
          textId = "textId"+i;
          var temp = document.getEle mentById(textId ).value;
          if(temp=="")
          alert('text '+i+' is null');
          }
          }
          </script>
          </head>
          <div id='myDiv'>
          </div>
          <br/>
          <input type="text" id='user'>
          <br/>
          <input type="Button" value="generate " onclick="create Text()"><br/>
          <input type="button" value="validate " onclick="valida teText()">
          </html>[/HTML]

          Pls Post back your feedback. If you need more help i can help you out

          Regards
          Ramanan Kalirajan

          Comment

          Working...