Validating 10 Textboxes using for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sriku
    New Member
    • Mar 2008
    • 6

    Validating 10 Textboxes using for loop

    I have created ten textboxes and name them as "txt1,txt2,..tx t10"
    (i.e just changing suffix by consecutive number).

    Now I have to validate these ten textboxes on Submit event (Say..No textbox should accept alphabet).

    I trying to use for loop to validate this i.e. trying to change the suffix of textbox name in each iteration.

    [CODE=javascript]for(var i=1;i<=10;i++)
    {
    if(document.myf orm.txt+i+.valu e==0)
    {
    alert("Cannot be empty!");
    }
    }[/CODE]

    But this is not working.

    So please give me the suitable code for my above requirement.
    Last edited by gits; Apr 29 '08, 05:57 AM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    first ... you have to refer correctly to the nodes:

    [CODE=javascript]for (var i = 1; i <= 10; i++) {
    if(document.myf orm[txt + i].value == 0) {
    alert("Cannot be empty!");
    }
    }[/CODE]
    kind regards

    Comment

    • vee10
      New Member
      • Oct 2006
      • 141

      #3
      hi...

      and this should be there to check if it is empty or not
      [CODE=javascript]if(document.myf orm["txt" + i].value =="")[/CODE]
      if defaultly u r assigning 0 then it should be 0

      hi gits.....

      i am getting error that it is null
      at document.myform

      myform is the id of the form
      [HTML]<form id="myform">[/HTML]

      Can u plz tell me why i am getting that





      Originally posted by gits
      hi ...

      first ... you have to refer correctly to the nodes:

      [CODE=javascript]for (var i = 1; i <= 10; i++) {
      if(document.myf orm[txt + i].value == 0) {
      alert("Cannot be empty!");
      }
      }[/CODE]
      kind regards
      Last edited by gits; Apr 29 '08, 07:30 AM. Reason: added code tags

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        of course ... it must be:

        [CODE=javascript]document.myform['txt' + i].value[/CODE]

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          and to check for 0 should work ... just try it out:

          [CODE=javascript]var val = 0 == '';[/CODE]
          here var val will be true ... since javascript converts the types to compare the values. in case you want to check for same types use the identity operator:

          [CODE=javascript]var val = 0 === '';[/CODE]
          here var val will get a false assigned.

          kind regards

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by vee10
            hi gits.....

            i am getting error that it is null
            at document.myform

            myform is the id of the form
            [HTML]<form id="myform">[/HTML]

            Can u plz tell me why i am getting that
            document.myform assumes a form named "myform", not with an id of "myform". So give your form a name "myform", or use document.getEle mentById("myfor m") to access the form.

            Comment

            • vee10
              New Member
              • Oct 2006
              • 141

              #7
              ok
              thanks for the reply


              Originally posted by acoder
              document.myform assumes a form named "myform", not with an id of "myform". So give your form a name "myform", or use document.getEle mentById("myfor m") to access the form.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by vee10
                ok
                thanks for the reply
                You're welcome.

                PS. please try to bottom-post (as I have done) rather than top-post. It makes it easier to follow.

                Comment

                Working...