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

Javascript coding ..

Moderators: macek, egami, gesf

nitapita
New php-forum User
New php-forum User
Posts: 8
Joined: Sun Jan 06, 2013 12:41 am

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

Postby nitapita » Sat May 04, 2013 11:31 pm

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: Select all

<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

nitapita
New php-forum User
New php-forum User
Posts: 8
Joined: Sun Jan 06, 2013 12:41 am

Re: Order Form - JS Alert when all the input fields values a

Postby nitapita » Sun May 05, 2013 2:14 am

Found the solution on another forum.

Code: Select all

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()">



Return to “JavaScript”

Who is online

Users browsing this forum: No registered users and 0 guests