Can someone/ANYONE please PRETTY PLEASE help me with my validation of form input?

I am trying to fix my form so that if a person doesn't fill out a field, it will NOT post to Mysql, but instead give an error message and allow the person to go back and input the information.

Here is what I have so far but I'm getting an error when the form is not filled out and I click submit:

Parse error: syntax error, unexpected T_VARIABLE in /home..........

/* Validation */

function check_field1($reference)
{
  if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\
   ]+$/s",$reference))
    return TRUE;
  else
    return FALSE;
}

function check_field2($amount)
{
  if(!preg_match("/[^0-9\ ]+$/",$amount))
    return TRUE;
  else
    return FALSE;
}

function check_field3($name)
{
  if(!preg_match("/[^0-9]+$/ ",$name))
    return TRUE;
  else
    return FALSE;
}

function check_field4($cardtype)
{
  if(!preg_match("/[^0-9]+$/ "$cardtype))
    return TRUE;
  else
    return FALSE;
}

function check_field5($cardnumber)
{
  if(!preg_match("/[^0-9]+$/ ",$cardnumber))
    return TRUE;
  else
    return FALSE;
}

function check_field6($exp)
{
  if(!preg_match("/[^0-9]+$/ ",$exp))
    return TRUE;
  else
    return FALSE;
}

function check_field7($cvc2)
{
  if(!preg_match("/[^0-9]+$/ ",$cvc2))
    return TRUE;
  else
    return FALSE;
}

function check_field8($ssn)
{
  if(!preg_match("/[^0-9]+$/ ",$ssn))
    return TRUE;
  else
    return FALSE;
}

function check_field9($address)
{
  if(!preg_match("/[^0-9]+$/ ",$address))
    return TRUE;
  else
    return FALSE;
}

function check_field10($city)
{
  if(!preg_match("/[^0-9]+$/ ",$city))
    return TRUE;
  else
    return FALSE;
}

function check_field11($state)
{
  if(!preg_match("/[^0-9]+$/ ",$state))
    return TRUE;
  else
    return FALSE;
}

function check_field11($$zip)
{
  if(!preg_match("/[^0-9]+$/ ",$zip))
    return TRUE;
  else
    return FALSE;
}

function check_field11($email)
{
  if(!preg_match("/[^0-9]+$/ ",$email))
    return TRUE;
  else
    return FALSE;
}

function check_field11($phone)
{
  if(!preg_match("/[^0-9]+$/ ",$phone))
    return TRUE;
  else
    return FALSE;
}


/* Validation */

$error=0; // check up variable

/* get it checking */

if(!check_field1($reference))
{
  echo "Illegal input $reference in 'Reference'";
  $error++; // $error=$error+1;
}
if(!check_field2($amount))
{
  echo "Illegal input $amount in 'Amount'";
  $error++;
}
if(!check_field3($name))
{
  echo "Illegal input $name in 'Name'";
  $error++;
}

if(!check_field1($cardtype))
{
  echo "Illegal input $cardtype in 'Cardtype'";
  $error++; // $error=$error+1;
}

if(!check_field1($cardnumber))
{
  echo "Illegal input $cardnumber in 'Cardnumber'";
  $error++; // $error=$error+1;
}

if(!check_field1($exp))
{
  echo "Illegal input $exp in 'Exp'";
  $error++; // $error=$error+1;
}

if(!check_field1($cvc2))
{
  echo "Illegal input $cvc2 in 'CVC2'";
  $error++; // $error=$error+1;
}

if(!check_field1($ssn))
{
  echo "Illegal input $ssn in 'SSN'";
  $error++; // $error=$error+1;
}

if(!check_field1($address))
{
  echo "Illegal input $address in 'Address'";
  $error++; // $error=$error+1;
}

if(!check_field1($city))
{
  echo "Illegal input $city in 'City'";
  $error++; // $error=$error+1;
}

if(!check_field1($state))
{
  echo "Illegal input $state in 'State'";
  $error++; // $error=$error+1;
}

if(!check_field1($zip))
{
  echo "Illegal input $zip in 'Zip'";
  $error++; // $error=$error+1;
}

if(!check_field1($email))
{
  echo "Illegal input $email in 'Email'";
  $error++; // $error=$error+1;
}

if(!check_field1($phone))
{
  echo "Illegal input $phone in 'Phone'";
  $error++; // $error=$error+1;
}

if(!check_field1($initals))
{
  echo "Illegal input $initals in 'Initals'";
  $error++; // $error=$error+1;
}


if($error == 0)
{
  echo
  "
 Thank You For Your Information!<p>
  Your data:<br>
  Your name: $your_name<br>
  Your phone: $your_phone<br>
  ZIP code: $your_zip
  ";
}else{
  echo "Number of errors: $error";
}

    Missing comma in between the preg_match() args:

    function check_field4($cardtype)
    {
      if(!preg_match("/[^0-9]+$/ "[color=red][b],[/b][/color] $cardtype))
        return TRUE;
      else
        return FALSE;
    }
    

      Here is my NEW file:

      / Validation /

      function check_field1($reference)
      {
      if(!preg_match("/[a-zA-Z0-9.-\Ä\ä\Ö\ö\Ü\ü\
      ]+$/s",$reference))
      return TRUE;
      else
      return FALSE;
      }

      function check_field2($amount)
      {
      if(!preg_match("/[0-9\ ]+$/",$amount))
      return TRUE;
      else
      return FALSE;
      }

      function check_field3($name)
      {
      if(!preg_match("/[0-9]+$/ ",$name))
      return TRUE;
      else
      return FALSE;
      }

      function check_field4($cardtype)
      {
      if(!preg_match("/[0-9]+$/ ",$cardtype))
      return TRUE;
      else
      return FALSE;
      }

      function check_field5($cardnumber)
      {
      if(!preg_match("/[0-9]+$/ ",$cardnumber))
      return TRUE;
      else
      return FALSE;
      }

      function check_field6($exp)
      {
      if(!preg_match("/[0-9]+$/ ",$exp))
      return TRUE;
      else
      return FALSE;
      }

      function check_field7($cvc2)
      {
      if(!preg_match("/[0-9]+$/ ",$cvc2))
      return TRUE;
      else
      return FALSE;
      }

      function check_field8($ssn)
      {
      if(!preg_match("/[0-9]+$/ ",$ssn))
      return TRUE;
      else
      return FALSE;
      }

      function check_field9($address)
      {
      if(!preg_match("/[0-9]+$/ ",$address))
      return TRUE;
      else
      return FALSE;
      }

      function check_field10($city)
      {
      if(!preg_match("/[0-9]+$/ ",$city))
      return TRUE;
      else
      return FALSE;
      }

      function check_field11($state)
      {
      if(!preg_match("/[0-9]+$/ ",$state))
      return TRUE;
      else
      return FALSE;
      }

      function check_field12($zip)
      {
      if(!preg_match("/[0-9]+$/ ",$zip))
      return TRUE;
      else
      return FALSE;
      }

      function check_field13($email)
      {
      if(!preg_match("/[0-9]+$/ ",$email))
      return TRUE;
      else
      return FALSE;
      }

      function check_field14($phone)
      {
      if(!preg_match("/[0-9]+$/ ",$phone))
      return TRUE;
      else
      return FALSE;
      }

      / Validation /

      $error=0; // check up variable

      / get it checking /

      if(!check_field1($reference))
      {
      echo "Illegal input $reference in 'Reference'";
      $error++; // $error=$error+1;
      }
      if(!check_field2($amount))
      {
      echo "Illegal input $amount in 'Amount'";
      $error++;
      }
      if(!check_field3($name))
      {
      echo "Illegal input $name in 'Name'";
      $error++;
      }

      if(!check_field4($cardtype))
      {
      echo "Illegal input $cardtype in 'Cardtype'";
      $error++; // $error=$error+1;
      }

      if(!check_field5($cardnumber))
      {
      echo "Illegal input $cardnumber in 'Cardnumber'";
      $error++; // $error=$error+1;
      }

      if(!check_field6($exp))
      {
      echo "Illegal input $exp in 'Exp'";
      $error++; // $error=$error+1;
      }

      if(!check_field7($cvc2))
      {
      echo "Illegal input $cvc2 in 'CVC2'";
      $error++; // $error=$error+1;
      }

      if(!check_field8($ssn))
      {
      echo "Illegal input $ssn in 'SSN'";
      $error++; // $error=$error+1;
      }

      if(!check_field9($address))
      {
      echo "Illegal input $address in 'Address'";
      $error++; // $error=$error+1;
      }

      if(!check_field10($city))
      {
      echo "Illegal input $city in 'City'";
      $error++; // $error=$error+1;
      }

      if(!check_field11($state))
      {
      echo "Illegal input $state in 'State'";
      $error++; // $error=$error+1;
      }

      if(!check_field12($zip))
      {
      echo "Illegal input $zip in 'Zip'";
      $error++; // $error=$error+1;
      }

      if(!check_field13($email))
      {
      echo "Illegal input $email in 'Email'";
      $error++; // $error=$error+1;
      }

      if(!check_field14($phone))
      {
      echo "Illegal input $phone in 'Phone'";
      $error++; // $error=$error+1;
      }

      if(!check_field15($initals))
      {
      echo "Illegal input $initals in 'Initals'";
      $error++; // $error=$error+1;
      }

      ?>

      But it's still NOT validating that the form has information in the fields!

      I don't know what to do.....

      When I submit a blank form it still posts and says

      Your Information Has Been AddedDuplicate entry '' for key 1

        I am not up to speed on php yet but the logic seems to be inverted.
        It looks like your check functions returns TRUE on empty strings...

        I need to test this myself, and yes it does.

        Therefore your error counter won't count up.

          Also, the nice thing about functions is having a single code block that you can call over and over again throughout your script. When you start making several functions that do the same thing, you're kind of defeating the purpose.

          Try:

          function check_field($thingtocheck)
          {
          if(!preg_match("/[^0-9\ ]+$/",$thingtocheck))
          return TRUE;
          else
          return FALSE;
          }
          
          //and call it using
          
          echo check_field($state);
          echo check_field($zip);
          echo check_field($ssn);
          
          
          

            Do I fill in the rest of the fields below the last one?

            function check_field($thingtocheck)
            {
            if(!preg_match("/[0-9\ ]+$/",$thingtocheck))
            return TRUE;
            else
            return FALSE;
            }

            //and call it using

            echo check_field($state);
            echo check_field($zip);
            echo check_field($ssn);

              Please tell me how it should look.
              I dont know what "Calling It" means.... (sorry)

              Please paste me how the complete text should look or tell me how to call it.

              I inputed this:

              //and call it using

              echo check_field($state);
              echo check_field($zip);
              echo check_field($ssn);

              under the last line in my validation text area in the php coding

              When I submitted a blank form I received this message:

              Fatal error: Call to undefined function: check_field() in

                Here is my new validation code however its still not validating there is text in the fields before it inserts the record into mysql.

                //Validation
                <?php

                if (isset($POST['reference']) && isset($POST['amount']) && isset($POST['name']) && isset($POST['cardtype']) && isset($POST['cardnumber']) && isset($POST['exp']) && isset($POST['cvc2']) && isset($POST['ssn']) && isset($POST['address']) && isset($POST['city'])&& isset($POST['state']&& isset($POST['zip'])&& isset($POST['email']) && isset($POST['phone']) && isset($_POST['initial']) {

                // Make a safe query
                $query = sprintf("INSERT INTO data('reference', 'amount', 'name', 'cardtype', 'cardnumber', 'exp', 'cvc2', 'ssn', 'address', 'city', 'state', 'zip', 'email', 'phone', 'initials') VALUES ('%s', '%s', %d)",
                mysql_real_escape_string($reference, $link),
                mysql_real_escape_string($amount, $link),
                mysql_real_escape_string($name, $link),
                mysql_real_escape_string($cardtype, $link),
                mysql_real_escape_string($cardnumber, $link),
                mysql_real_escape_string($exp, $link),
                mysql_real_escape_string($cvc2, $link),
                mysql_real_escape_string($ssn, $link),
                mysql_real_escape_string($address, $link),
                mysql_real_escape_string($city, $link),
                mysql_real_escape_string($state, $link),
                mysql_real_escape_string($zip, $link),
                mysql_real_escape_string($email, $link),
                mysql_real_escape_string($phone, $link),
                mysql_real_escape_string($initial, $link),
                $_POST['authorize.php']);

                    mysql_query($query, $link);
                
                    if (mysql_affected_rows($link) > 0) {
                        echo "Card Information Recieved\n";
                    }
                }

                } else {
                echo "Fill the form properly\n";
                }

                  I have NEW VALIDATION CODE TEXT and I am trying to make it work with my form.
                  My form is inserting in mysql without verifying there is text in the fields.

                  PLEASE help me solve this. I need this up for my site like 2 weeks ago!...lol...*cry

                  Here is the validation text that I am using for my form:
                  P.S. Does my form have to be in .html? because it's in .php

                  //Validation
                  <?php

                  if (isset($POST['reference']) && isset($POST['amount']) && isset($POST['name']) && isset($POST['cardtype']) && isset($POST['cardnumber']) && isset($POST['exp']) && isset($POST['cvc2']) && isset($POST['ssn']) && isset($POST['address']) && isset($POST['city'])&& isset($POST['state']&& isset($POST['zip'])&& isset($POST['email']) && isset($POST['phone']) && isset($_POST['initial']) {

                  // Make a safe query
                  $query = sprintf("INSERT INTO data('reference', 'amount', 'name', 'cardtype', 'cardnumber', 'exp', 'cvc2', 'ssn', 'address', 'city', 'state', 'zip', 'email', 'phone', 'initials') VALUES ('%s', '%s', %d)",
                  mysql_real_escape_string($reference, $link),
                  mysql_real_escape_string($amount, $link),
                  mysql_real_escape_string($name, $link),
                  mysql_real_escape_string($cardtype, $link),
                  mysql_real_escape_string($cardnumber, $link),
                  mysql_real_escape_string($exp, $link),
                  mysql_real_escape_string($cvc2, $link),
                  mysql_real_escape_string($ssn, $link),
                  mysql_real_escape_string($address, $link),
                  mysql_real_escape_string($city, $link),
                  mysql_real_escape_string($state, $link),
                  mysql_real_escape_string($zip, $link),
                  mysql_real_escape_string($email, $link),
                  mysql_real_escape_string($phone, $link),
                  mysql_real_escape_string($initial, $link),
                  $_POST['authorize.php']);

                      mysql_query($query, $link);
                  
                      if (mysql_affected_rows($link) > 0) {
                          echo "Card Information Recieved\n";
                      }
                  }

                  } else {
                  echo "Fill the form properly\n";
                  }

                    *******PS. I do NOT have a field called "link" should this be there "$link" ???

                    Example: mysql_real_escape_string($initial, $link),

                      Use !empty($variable) instead of isset($variable). However, if a zero is a legal value for any field, then instead you'll need to use isset($variable) && trim($variable) !== "" (note the "!==" as opposed to just "!=").

                      PS: You really need to discover this forum's

                       tag instead of using the [quote] tag for code samples.

                        PPS: I have merged the two threads together into one here.

                          Your check_field functions are returning TRUE on empty strings and therefore will your evaluation test fail to see that the string is empty.

                          Scriptmaker were saying that you would benefit from just using one function instead of 14.
                          Since the difference between them is just the regex string you should have that as an argument.

                          It would then look like:

                          $regExStr1 = "/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\]+$/s";
                          $regExStr2 = "/[^0-9\ ]+$/"; 
                          function check_is_field_valid($regexString, $inpString)
                          {
                          if(!preg_match($regexString, $inpString))
                          return FALSE;
                          else
                          return TRUE;
                          } 
                          
                          /* Validation */
                          
                          $error=0; // check up variable
                          
                          /* get it checking */
                          
                          if(!check_is_field_valid($regExStr1, $reference))
                          {
                          echo "Illegal input $reference in 'Reference'";
                          $error++; // $error=$error+1;
                          }
                          if(!check_is_field_valid($regExStr2, $amount))
                          {
                          echo "Illegal input $amount in 'Amount'";
                          $error++;
                          }
                          ..... 
                          

                          and so forth to keep your previous checks the same.

                            I must TOTALLY be missing something here....both in my head and in my .php validation text.

                            Here is what my current code looks like from validation text to the end of complete file.

                            (P.S. *Note how I found the php tag feature instead of quote feature =))

                             /* Validation */
                            
                            function check_field1($reference)
                            {
                            if(!preg_match("/[^a-zA-Z0-9
                            ]+$/s",$reference))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            function check_field2($amount)
                            {
                            if(!preg_match("[0-9.",$amount))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            
                            function check_field3($name)
                            {
                            if(!preg_match("/[^a-zA-Z0-9
                            ]+$/s",$name))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            function check_field4($cardtype)
                            {
                            if(!preg_match("/[^a-zA-Z0-9.
                            ]+$/s ",$cardtype))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            function check_field5($cardtype)
                            {
                            if(!preg_match("/[^a-zA-Z0-9
                            ]+$/s ",$cardtype))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            function check_field6($cardnumber)
                            {
                            if(!preg_match("/[^a-zA-Z0-9-
                            ]+$/s ",$cardnumber))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            function check_field7($exp)
                            {
                            if(!preg_match("/[^a-zA-Z0-9
                            ]+$/s ",$exp))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            function check_field8($cvc2)
                            {
                            if(!preg_match("/[^a-zA-Z0-9
                            ]+$/s ",$cvc2))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            function check_field9($ssn)
                            {
                            if(!preg_match("/[^a-zA-Z0-9-
                            ]+$/s ",$ssn))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            function check_field10($address)
                            {
                            if(!preg_match("/[^a-zA-Z0-9\.
                            ]+$/s ",$address))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            function check_field11($city)
                            {
                            if(!preg_match("/[^a-zA-Z0-9
                            ]+$/s ",$city))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            function check_field12($state)
                            {
                            if(!preg_match("/[^a-zA-Z0-9.-
                            ]+$/s",$state))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            function check_field13($zip)
                            {
                            if(!preg_match("/[^a-zA-Z0-9.
                            ]+$/s ",$zip))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            function check_field14($email)
                            {
                            if(!preg_match("/[^a-zA-Z0-9
                            ]+$/s@.",$email))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            function check_field15($phone)
                            {
                            if(!preg_match("/[^a-zA-Z0-9\-.
                            ]+$/s ",$phone))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            function check_field16($initial)
                            {
                            if(!preg_match("/[^a-zA-Z0-9.",$initial))
                            return TRUE;
                            else
                            return FALSE;
                            }
                            
                            //Something else to add
                            $regExStr1 = "/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\]+$/s";
                            $regExStr2 = "/[^0-9\ ]+$/";
                            function check_is_field_valid($regexString, $inpString)
                            {
                            if(!preg_match($regexString, $inpString))
                            return FALSE;
                            else
                            return TRUE;
                            } 
                            
                            /* Validation */
                            
                            $error=0; // check up variable
                            
                            
                            
                            /* get it checking */
                            
                            if(!empty($reference) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$reference)) 
                            
                            if(!empty($amount) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$amount)) 
                            
                            if(!empty($name) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$name)) 
                            
                            if(!empty($cardtype) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$cardtype)) 
                            
                            if(!empty($cardnumber) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$cardnumber)) 
                            
                            if(!empty($cvc2) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$cvc2)) 
                            
                            if(!empty($exp) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$exp)) 
                            
                            if(!empty($ssn) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$ssn)) 
                            
                            if(!empty($address) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$amount)) 
                            
                            if(!empty($city) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$city)) 
                            
                            if(!empty($state) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$state)) 
                            
                            if(!empty($zip) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$zip)) 
                            
                            if(!empty($email) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$amount)) 
                            
                            if(!empty($phone) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$phone)) 
                            
                            if(!empty($initial) and !preg_match("/[^a-zA-Z0-9
                            ]+$/s",$initial)) 
                            
                            ?>	

                            I SO appreciate your helping me and you can email me your website links and I can add them to my popular website for hits/visitors. No return link needed.

                              I don't understand what you're trying to accomplish now. It looks like you have a ton of functions that you don't even use.

                              for example, what is the purpose of this?:

                              if(!empty($reference) and !preg_match("/[^a-zA-Z0-9
                              ]+$/s",$reference))
                              
                              if(!empty($amount) and !preg_match("/[^a-zA-Z0-9
                              ]+$/s",$amount))

                              Use the "something to add" code to eliminate all of those redundant (unnecessary) functions, and use the code that igorlopez gave you.

                              Basically, he gave you a code that defines two different regex strings. Then he gave you a function that requires two inputs (the reg expression string, the variable you are trying to validate). So, if you want to validate, say $reference, you would send the function ($regexString1,$reference). However, if you wanted to validate $city, you would send it ($regexString2,$city).

                                Also preg_match will return a number 0 if criteria is not found and false if there was an error. try something like this

                                function check_field1($reference) {
                                if(preg_match("/[^a-zA-Z0-9]+$/s",$reference) != 0)
                                return TRUE;
                                else
                                return FALSE;
                                }
                                

                                Also you should create one function to check your fields.

                                  Write a Reply...