How to getElementByType and check validation using javascript.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lucoin
    New Member
    • Sep 2007
    • 24

    How to getElementByType and check validation using javascript.

    Hello guys,
    I met a problem about php and javascipt
    For a tickets booking system, when a customer search for flights from one city, so in the data base there should be many routes to different cities, like from Sydney, it can go to Landon, Paris and Beijing and etc. So first I use php to get the data from data base, and use a loop to list all of them and after each route, there is a input field which type is text for the customer to enter the tickets quantities they want to by, and I name the input fields by using the loop, from i=1 to i='number of records' . Now the problem is I have to check the validation of the entered quantities be from 1 to 6, which means the customer just can buy six tickets for one routes. how can I get the variables from the form, because I also use some inputs fields to transfer other data.this is how I define the input fields in php:[PHP] print "<input type=\"text\" id=\"$i\" name=\"ticket_$ i\" size=\"3\" value=\"\">";[/PHP]
    How can I use Javascript to check the validation of this fields?

    Many thanks first.
  • lucoin
    New Member
    • Sep 2007
    • 24

    #2
    I mean if I use getElementByTyp e('text'), what variables I will get. Thank you.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      hi ...

      as far as i know there is no standard dom method that is called getElementsByTy pe() ... so you have to implement it or may be you have copied one for you ... the standard way is to use:

      [CODE=javascript]
      var node_list = document.getEle mentsByTagName( 'input');

      for (var i = 0; i < node_list.lengt h; i++) {
      var node = node_list[i];

      if (node.getAttrib ute('type') == 'text') {
      // do something here with a <input type="text" .../>
      // we alert its value here
      alert(node.valu e);
      }
      }
      [/CODE]
      kind regards

      Comment

      • lucoin
        New Member
        • Sep 2007
        • 24

        #4
        Originally posted by gits
        hi ...

        as far as i know there is no standard dom method that is called getElementsByTy pe() ... so you have to implement it or may be you have copied one for you ... the standard way is to use:

        [CODE=javascript]
        var node_list = document.getEle mentsByTagName( 'input');

        for (var i = 0; i < node_list.lengt h; i++) {
        var node = node_list[i];

        if (node.getAttrib ute('type') == 'text') {
        // do something here with a <input type="text" .../>
        // we alert its value here
        alert(node.valu e);
        }
        }
        [/CODE]
        kind regards
        Thank you, it works very well. But one more thing, how can I get how many 'text' fields there?

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          hi ...

          we may write a get_textboxes method like this:

          [CODE=javascript]
          function get_textboxes() {
          var node_list = document.getEle mentsByTagName( 'input');
          var textboxes = [];

          for (var i = 0; i < node_list.lengt h; i++) {
          var node = node_list[i];

          if (node.getAttrib ute('type') == 'text') {
          textboxes.push( node);
          }
          }

          return textboxes;
          }
          [/CODE]

          so now you call the function and you get an array with textboxes out of it, and you can ask for its length.

          kind regards

          Comment

          • Jervy

            #6
            Thanks, it worked like a charm!
            cheers man...

            Comment

            • ramonlm
              New Member
              • Mar 2016
              • 1

              #7
              Beautiful... Many thanks!!!

              Comment

              Working...