Jump to content

Functions for my Form - Not sure if they are correct or not....


kenwvs

Recommended Posts

These are the functions that I am using in my form.  I am not sure if I have written them correctly or not, as I got the gist of it from a tutorial at another site, and then went out on my own.  If you want to browse and let me know where I have gone wrong that would be great.

I have a feeling this tutorial that is for form validation may not have taken the most direct route, so it may not look totally normal.

Thanks in advance,

Ken

[code]
<?php
function isDigits($element) {                      //numbers only
  return !preg_match ("/[^0-9]/", $element);
}
?>

<?php
function Dollars($element) {                        //numbers and decimal
  return !preg_match ("/[^0-9]./", $element);
}
?>

<?php
function isLetters($element) {                      //letters only
  return !preg_match ("/[^A-z]/", $element);
}
?>

<?php
function LetandNumOnly($element) {                  //Letters and Numbers
  return !preg_match ("/[^A-z0-9]/", $element);    //with No Spaces
}
?>

<?php
function LettersAndDigits($element) {              //Letters, Numbers
  return !preg_match ("/[^A-z0-9 ]/", $element);  //and Spaces
}
?>

<?php
function Variable($element) {                      //letters, numbers, spaces
  return !preg_match ("/[^A-z0-9,. ]/", $element);//commas and periods only
}
?>

<?php
function checkLength($string, $min, $max) {        //Check the Length
  $length = strlen ($string);                      //min and max
  if (($length < $min) || ($length > $max)) {
    return FALSE;
  } else {
    return TRUE;
  }
}
?>

<?php
function checkMailCode($code, $country) {          //Check Postal Code
  $code = preg_replace("/[\s|-]/", "", $code);      //by Country
  $length = strlen ($code);
  switch (strtoupper ($country)) {
    case 'US':
      if (($length <> 5) && ($length <> 9)) {
        return FALSE;
      }
      return isDigits($code);
    case 'CA':
      if ($length <> 6) {
        return FALSE;
      }
      return preg_match ("/([A-z][0-9]){3}/", $code);
  }
}
?>

<?php
function checkURL($url) {                          //check valid URL Format
  return preg_match ("/http:\/\/(.*)\.(.*)/i", $url);
}
?>

<?php
function checkURLandConnect($url) {                //Check Valid URL and
  if (!preg_match ("/http:\/\/(.*)\.(.*)/i", $url)) {//Confirm by Connecting
    return FALSE;
  }
  $parts = parse_url($url);
  $fp = fsockopen($parts['host'], 80, $errno, $errstr, 10);
  if(!$fp) {
    return FALSE;
  }
  fclose($fp);
  return TRUE;
}
?>

<?php
function checkEmail($email) {                      //Check Email Format
  $pattern = "/^[A-z0-9\._-]+"
        . "@"
        . "[A-z0-9][A-z0-9-]*"
        . "(\.[A-z0-9_-]+)*"
        . "\.([A-z]{2,6})$/";
  return preg_match ($pattern, $email);
}
?>

<?php
function EmailorEmpty($email) {                    //Check Email Format
    if(empty($email)) {                            //or empty field
        return true;
    } else {
        $pattern = "/^[A-z0-9\._-]+"
        . "@"
        . "[A-z0-9][A-z0-9-]*"
        . "(\.[A-z0-9_-]+)*"
        . "\.([A-z]{2,6})$/";
        return preg_match ($pattern, $email);
    }
}
?>

<?php
function checkPassword($password) {                //check password for minimum
  $length = strlen ($password);                    //of 8 characters and must
  if ($length < 8) {                                //have a number between letters
    return FALSE;                                  //and a variation of letters
  }
  $unique = strlen (count_chars ($password, 3));
  $difference = $unique / $length;
  echo $difference;
  if ($difference < .60) {
    return FALSE;
  }
  return preg_match ("/[A-z]+[0-9]+[A-z]+/", $password);
}
?>

<?php
function BidIncrement($bid)  {
$bid_increment = $_POST['bid_increment'];  //function for bid_increment only
$item_type = $_POST['item_type'];
if($item_type == "auction" or $item_type=="dutch auction")
{
if(!(is_numeric($bid_increment))
OR empty($bid_increment)){
  echo "You have not entered a valid Bid Increment";
  exit;
  }
  else{
  echo "Valid bid increment";
  }
}elseif($item_type == "fixed price" OR $item_type="classified"){
  if(!empty($bid_increment)){
    echo "Bid Increments are not valid for this type of Listing!";
}
}

?>

<?php
function ReservePrice($reserve)    {
$reserve_price = $_POST['reserve_price'];  //function for reserve_price only
$item_type = $_POST['item_type'];
if($item_type == "Auction" or $item_type=="Ddutch Auction")
{
if(!(is_numeric($reserve_price))
OR empty($reserve_price)){
  echo "You have not entered a Valid Reserve Bid or Starting Price";
  exit;
  }
  else{
  echo "Valid bid increment";
  }
}elseif($item_type == "Fixed Price" OR $item_type="Classified Ad"){
  if(!empty($reserve_price)){
    echo "A Reserve Bid or Starting Price cannot be entered for this auction type";
}
}

?>
<?php
function ItemCategory($category)    {          // Verify Valid Category
$item_category = $_POST['item_category'];
if($item_category == "000")//assumes you gave it the value 000
{
echo "Please select a valid category";
exit;
}
}
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.