SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Order Form - JS Alert when all the input fields values are 0

    Hi Everyone.

    I'm working on a basic ordering form. What i'm trying to achive is to get javascript to return alert when all the inputs fields are with values 0.
    At least 1 of the inputs has to be filled in with value more then 0 in order to proceed with a ordering form.
    I have to mention that products list / input fields are generated dynamicly so their number might vary.

    Basic code:

    Code:
    <form name='packaging' action="packaging.php" method='post'>
    
    <?
    $result = mysql_query("SELECT * FROM packaging_items") or die(mysql_error());
                    while($row=mysql_fetch_array($result)) {
    echo "
    <input type='text' style='width:20px'  name='$row[prodno]' id='$row[prodno]' maxlength='4' value="0" onblur="if (this.value == '') {this.value = '0';}" onfocus="if (this.value == '0') {this.value = '';}" onkeypress='validate(event)' >
    ";
    }
    ?>
    
    </form>
    Any suggetions .. i really have no clue where to start ...

    Thank you in advance

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found the solution on another forum.

    Code:
    function validateForm(){
      var inputs =  document.getElementsByTagName('input');
      var noneZeroFound = false;
      for(var i=0;i< inputs.length;i++){
          var input = inputs[i];
          if(input.value != '0'){
            noneZeroFound = true;
            break;
          }
      }
      if(!noneZeroFound ){
          alert('MUST ENTER VALUE...');
          return false;
      }
     
      return true;
    }
     
    <form name='packaging' action="packaging.php" method='post' onSubmit="return validateForm()">

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Thank you for taking the tie to let us know you got it sorted

    I found your question on Stack Overflow, as well as your comments that it doesn't work.
    Did you get your problems sorted out?

    If so, it might be a good idea to accept the answer on S.O.

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi timing i guess, yes i posted a solution, with SO i wish to close it as solved. Problem is i still do not have a right to do so... but yesy is sorted and solution i posted above...

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Just remember to get rid of the debugging alert call before your code is made live. You don't want visitors to your page being able to check the checkbox in the alert that either disables JavaScript or disables alerts depending on the browser.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •