Jump to content

Archived

This topic is now archived and is closed to further replies.

kenwvs

A function for form validation - if this happens do that ... RESOLVED

Recommended Posts

Hi

The form I am working on and trying to validate is for an online auction when you want to list multiple items at once.  Basically all the fields needed to be completed are on the form, and then the form goes on to list the items.

In a couple of the fields, I need a more complicated validation, and not sure if it is possible.  It is basically like this...

Validate a field called bid_increment:
If the item_type is auction or dutch auction then bid_increment must be filled in with a dollar amount
If the item_type is fixed price or classified then bid_increment must be left empty.

** The item_type is a drop down box and the bid_incrment is a text box

I am not sure if it is possible to write a function like this.

I also have a drop down box for item_category.  The default answer for this box is "Please select category"  Can a function be written to allow anything except "Please Select Category", as they have then selected a valid category.

Thanks,

Ken

Share this post


Link to post
Share on other sites
Yes that is all possible:
the first part:
[code]
<?php
$bid_increment = $_POST['bid_increment'];
$item_type = $_POST['item_type'];
if($item_type == "auction" or $item_type=="dutch auction")
{
if(!(is_numeric($bid_increment) && is_int($bid_increment + 0)) OR empty($bid_increment)){
  echo "Invalid bid increment";
  exit;
  }
  else{
  echo "Valid bid increment";
  }
}elseif($item_type == "fixed price" OR $item_type=="classified"){
  if(!empty($bid_increment)){
    echo "you can have no bid increment with this item type!";
}
}
?>
[/code]

This assumes that you have given the options within the dropdown box the same value as i have e.g.:

<option value="auction">Auction</option>
<option value="dutch auction">Dutch Auction</option>
etc

Share this post


Link to post
Share on other sites
And for the second, again it depends on how you name the options in the dropdown box
[code]
<?php
$item_category = $_POST['item_category'];
if($item_category == "0")//assumes you gave it the value 0
{
echo "no category selected";
exit;
}
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.