Jump to content


Photo

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


  • Please log in to reply
2 replies to this topic

#1 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 13 July 2006 - 11:07 AM

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

#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 13 July 2006 - 11:27 AM

Yes that is all possible:
the first part:
<?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!";
}
}
?>

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

#3 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 13 July 2006 - 11:37 AM

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users