That's quite weird logic. Are you sure this does what you think?

if(count($this->errors) >= 0) {
return true;
}
else
{
return false;
}

    @ I'm not sure to be honest because I can't get any of my code to work properly. I may have to switch some stuff around but the idea behind is if the number of errors is 0 then return true if the errors are more than 0 return false. But I'm not sure what to do after that....

      Your code doesn't do what you just explained. Wrong comparison operator or wrong value compared.
      What you meant to code is:

      if(count($this->errors) > 0) {
      return true;
      }
      else
      {
      return false;
      } 

      OR

      if(count($this->errors) >= 1) {
      return true;
      }
      else
      {
      return false;
      } 

      This might have contributed to your problem.

      http://php.net/manual/en/language.operators.comparison.php

        Did you wash your hands after? 😉

        You don't need to delete the code. Just fix it. I'm a bit busy with my own coding at the moment but I'll try reading through yours (please edit and add the syntax codes so please can read it without pulling their eyes out of their socket) after work.

          @ OK I re-added the code I deleted and I fixed the bad logic issues.
          @Everyone I'm going to post my code correctly this time so people don't want to kill themselves or me when looking at the code lol

          This code is in the validateForm.php:

           
          <?php
          class validateForm {
          
          //-------------------------------------------------------------------
          // parameters
          //-------------------------------------------------------------------
          
          /**
          *Array to hold the errors
          *
          *@access public
          *@var errors array
          *@var required array 
          */
          public $errors = array();
          
          //------------------------------------------------------------------
          // validation methods
          //------------------------------------------------------------------
          
          /**
          * Validates name fields
          *
          *@access public
          *@param  
          */ public function validateName($FamilyName) { if(strlen($FamilyName) <= 0 || strlen($FamilyName) >= 15 ) { $this->setError($FamilyName, "Please enter a last name and it must be no longer than 15 characters"); } else if(preg_match("/^[a-zA-Z\\-\\., \']+$/")) { $this->setError($FamilyName, "Last name must consist only of letters A-Z, ' and - "); } else { return true; } } /*function validateAddress($curAddress){ if(preg_match("/^[a-zA-Z0-9 _-.,:']+$/") <= 0){ return true; } else { return false; //error message to tell user either its wrong or wasn't entered } } /*function validateUSAZip($PostalCode){ if(preg_match("/^([0-9]{5})(-[0-9]{4})?$/i") === 0){ return true; } else { return false; //error message to tell user either its wrong or wasn't entered } } /*function validateEmail($EmailAddress){ if(preg_match("/^\S+@[w\d.-]{2,}\.[\w]{2,6}$/iU") === ""){ return true; } else { return false; //error message to tell user either its wrong or wasn't entered } } /*function validatePhoneNum($phoneNumber){ if(trim($phoneNumber) === " "){ $errmsg = "Please enter a phone number."; } else if(!preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/s")){ $errmsg = 'Please enter a valid phone number.'; return true; } }*/ /*$query = sprintf("INSERT INTO tbl (FamilyName, GivenName) Values (%s, %s)", mysqli_real_escape_string($link, $FamilyName), mysqli_real_escape_string($link, $GivenName) );*/ //--------------------------------------------------------- // Error handling methods //--------------------------------------------------------- /** * sets an error message for a form element * @access private * @param string $element - name of the form element * @param string $message - error message to be displayed * return void */ private function setError($element, $errors){ $this->errors[$element] = $errors; }//end logError /** * returns the error of a single form element * * @access public * @param string $elementName - name of the form element * @return string */ public function getError($elementName){ if($this->errors[$elementName]){ return $this->errors[$elementName]; } else { return false; } }//end getError /** * displays the errors as an html un-ordered list * *@access public *@return string: A html list of the forms errors */ public function displayErrors(){
          $errorsList = "<ul class=\"errors\">\n"; foreach($this->errors as $value) { $errorsList .= "<li>". $value . "</li>\n"; } $errorsList .= "<\ul>\n"; return $errorsList; }//end displayErrors /** *returns whether the form has errors * *@access public *@return boolean */ public function hasErrors() { if(count($this->errors) > 0) { return true; } else { return false; } }//end hasErrors /** * returns a string stating how many errors there were * *@access public *@return void */ public function errorNumMessage() { if(count($this->errors) >= 1) { $message = "There are " . count($this->errors) . " errors in the form!\n"; } else { return true; } }//end errorNumMessage }//end validateForm class exit; ?>

          This PHP code on the form side:

          <?php
          
          if(isset($_POST['submit'])) {
          
          include('validateForm.php');
          
          //Create post data to variables from each HTML form element
          $FamilyName = trim($_POST["FamilyName"]);
          $GivenName = trim($_POST["GivenName"]);
          $MiddleInitial = $_POST["MiddleInitial"];
          $curAddress = $_POST["curAddress"];
          $Municipality = $_POST["Municipality"];
          $Region = $_POST["Region"];
          $PostalCode = $_POST["PostalCode"];
          $phoneNumber = $_POST["phoneNumber"];
          $EmailAddress = $_POST["EmailAddress"];
          $legalYes = $_POST["legalYes"];//radio button
          $legalNo = $_POST["legalNo"];//radio button
          //Took off the rest of the POST variables for the time being to keep everyone sane! 
          
          //start validating our form
          $v = new validateForm();
          $v->validateName($FamilyName, "FamilyName");
          
          //Counts up all the errors 
          if (!$v-hasErrors()) {               <----- Not sure how to change it to do what I would like it to do
          //Sets the number of errors 
          $message = $v->errorNumMessage();
          //Store the errors list in a variable
          $errors = $v->displayErrors();
          //get the individual error messages
          $FamilyName = $v->getError("FamilyName");
          }else{
          return true;
          }
          }
          ?>
          

          OK this evening I have my one of my programming classes so I'm going to be asking my teacher for help now hopefully he will be helpful! I'm also going to ask him to see if its possible to display errors using a mouse hover on a input field that has an error using php instead of creating a div due to the design structure of my form. I know from researching on the web it can be done in javascript/jquery but no solid answer so far but Ill worry about that after getting the validation to start working lol.

            @ Did I forgot to add the ">"? Or is it just the wrong syntax?

            if (!$v-hasErrors()) { 

              OK, what I'd do is define an array with mandatory fields definition and type/method to check their validity.

              $mandatory_fields['phoneNumber'] = 'checkPhoneNumber';
              $mandatory_fields['curAddress'] = 'checkCurAddress';

              then just foreach the $mandatory_fields array instead of going through the long and tedious setting of a gorillion (1060 ROAAR!) variables.

              Optional task would be to define an error message array to have a nice reference and call only the message IDs.

              Next, I'd create an error handling class to separate the two artefacts (form validation and error handling) because I am worth it.

              Followed by a fix of the code bellow

              if (!$v-hasErrors()) { 

              becomes

              if (!$v->hasErrors()) { 

              Then go to bed with some allergy meds. But that part is just me.

                LOL! Thanks for the help, Bunkermaster, I get the idea of what needs to be done. Now the fun part is I'll just have to figure out the coding syntax and structure to make it work properly LOL.

                  We were all at that point one day (circa 1999 for me) and that's where the fun starts indeed. We're here to help you along the way if needed.

                  Use the source Luke!

                    OK I tried testing my code and when I hit the submit button it goes from my employment application page to my validation page but then it goes completely blank??? I'm figuring that I either need to put my code all in the HTML form page so it re-submits back to itself. Or I need to set-up a <div> element to display my errors (not sure if I set it up properly though). Or my error array is not functioning properly. Or its most likely something I don't have a clue on (I'm going with option 4:p).

                    Code from employment form:

                    <?php
                    
                    if(isset($_POST['submit'])) 
                    {	
                    	//import the validation library
                    	include('validateForm.php');
                    
                    $mandatory_fields[] = array(); //stores the validation rules
                    $errors[] = array(); //stores the errors
                    
                    //Create post data to variables from each HTML form element
                    $FamilyName = trim($_POST["FamilyName"]);
                    $GivenName = trim($_POST["GivenName"]);
                    $MiddleInitial = trim($_POST["MiddleInitial"]);
                    $curAddress = trim($_POST["curAddress"]);
                    $Municipality = trim($_POST["Municipality"]);
                    $Region = trim($_POST["Region"]);
                    $PostalCode = trim($_POST["PostalCode"]);
                    $phoneNumber = trim($_POST["phoneNumber"]);
                    $EmailAddress = trim($_POST["EmailAddress"]);
                    $legalYes = trim($_POST["legalYes"]);//radio button
                    $legalNo = trim($_POST["legalNo"]);//radio button
                    //cut out the other post variables to save space for posting.....
                    
                    //Standard form fields
                    $mandatory_fields['FamilyName'] = 'isAlphabet';
                    $mandatory_fields['GivenName'] = 'isAlphabet';
                    $mandatory_fields['MiddleInitial'] = 'isAlphabet';
                    $mandatory_fields['curAddress'] = 'validateAddress';
                    
                    /*foreach ($_POST as $fieldname => $value) {
                    	if(isset($_POST['fieldname'])) {
                    
                    }
                    }
                    
                    */
                    //start validating our form
                    $v = new validateForm();
                    $v->isAlphabet($FamilyName, "FamilyName");
                    
                    
                    //Counts up all the errors 
                    if (!$v->hasErrors()) {
                    	//Sets the number of errors 
                    	$message = $v->errorNumMessage();
                    	//Store the errors list in a variable
                    	$errors = $v->displayErrors();
                    	//Get the individual error messages
                    	$FamilyName = $v->getError("FamilyName");
                    	}
                    	else
                    	{
                    	return true;
                    	}
                    }
                    ?>
                    

                    Code from PHP validation page:

                    <?php
                    class validateForm {
                    
                    //------------------------------------------------------------------
                    // validation methods
                    //------------------------------------------------------------------
                    
                    /**
                    * Validates alphabet fields
                    *
                    *@access public
                    *@param  
                    */ public function isAlphabet($elementName) { if(strlen($elementName) <= 0) { $this->setError($elementName, "Please enter something into the text field."); } else if(preg_match("/^[a-zA-Z\\-\\., \']+$/")) { $this->setError($elementName, "This field must consist only of letters A-Z, ' and - ."); } else { return true; } } /*function validateAddress($curAddress){ if(strlen($curAddress) <= 0 || strlen($curAddress) >= 50) { $this->setError($curAddress, "Please enter address and must be no longer than 50 characters"); } else if(preg_match("/^[a-zA-Z0-9 -.,:']+$/")) { $this->setError("Address can only consist of letters A-Z, numbers 0-9, spaces, - , ",", :, and '."); } else { return true; } } function validateZipcode($PostalCode){ if(strlen($PostalCode) <= 0 || strlen($PostalCode) >= 8) { $this->setErrors($PostalCode, "Please enter a zip code in and can only be a max of 8 characters."); } else if(preg_match("/^([0-9]{5})(-[0-9]{4})?$/i")){ $this->setErrors("Zip code can only contain numbers with the format of xxxxx or xxxxx-xxxx"); } else { return true; } } public function validateEmail($EmailAddress){ if(strlen($EmailAddress) <= 0) { $this->setError($EmailAddress, "Please enter in an email address"); } else if(preg_match("/^\S+@[w\d.-]{2,}\.[\w]{2,6}$/iU")){ $this->setError($EmailAddress, "Please enter in a valid email address"); } else {
                    return true; } } function validatePhoneNum($phoneNumber){ if(strlen($phoneNumber) <= 0 || strlen($phoneNumber) >= 10){ $this->setErrors($phoneNumber, "Please enter a phone number that can only be a max of 10 characters long."; } else if(!preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/s")){ $this->setErrors($phoneNumber, "Please enter a valid phone number."); } else { return true; } } $query = sprintf("INSERT INTO tbl (FamilyName, GivenName) Values (%s, %s)", mysqli_real_escape_string($link, $FamilyName), mysqli_real_escape_string($link, $GivenName) ); */ //--------------------------------------------------------- // Error handling methods //--------------------------------------------------------- /** * sets an error message for a form element * @access private * @param string $element - name of the form element * @param string $message - error message to be displayed * return void */ private function setError($elementName, $errors){ $this->errors[$elementName] = $errors; }//end logError /** * returns the error of a single form element * * @access public * @param string $elementName - name of the form element * @return string */ public function getError($elementName){ if($this->errors[$elementName]){ return $this->errors[$elementName]; } else { return false; } }//end getError /** * displays the errors as an html un-ordered list * *@access public *@return string: A html list of the forms errors */ public function displayErrors(){ $errorsList = "<ul class=\"errors\">\n"; foreach($this->errors as $value) { $errorsList .= "<li>". $value . "</li>\n"; } $errorsList .= "<\ul>\n"; return $errorsList; }//end displayErrors /** *returns whether the form has errors * *@access public *@return boolean */ public function hasErrors() { if(count($this->errors) > 0) { return true; } else { return false; } }//end hasErrors /** * returns a string stating how many errors there were * *@access public *@return void */ public function errorNumMessage() { if(count($this->errors) >= 1) { $message = "There are " . count($this->errors) . " errors in the form!\n"; } else { return true; } }//end errorNumMessage }//end validateForm class exit; ?>

                    @ On the mandatory_fields array and creating a foreach statement to handle each variable I'm not sure how to go about coding it syntax-wise but I decided to do it the long way so that I can at least test out my validation functions to see that they're working properly. I found a decent example of the concept you mentioned but it was different in certain ways (PHP example: https://github.com/benkeen/php_validation).

                      InsideVoid;11008903 wrote:

                      then it goes completely blank???

                      Sounds like there were no errors in the form data you sent then. After all, you're not telling PHP to do anything if that's the case:

                      //Counts up all the errors 
                      if (!$v->hasErrors()) {
                      //brad: ...
                          }
                          else
                          {
                          return true; //brad: in other words, don't output anything - just quit silently
                          }

                        @ Well i tried to change the true to false and still got a blank page and I'm completely lost on what is wrong. I think I have to call one of the other error functions but I'm not sure how or which one to be honest?

                          InsideVoid;11008939 wrote:

                          Well i tried to change the true to false and still got a blank page

                          Again, that should be as expected. It doesn't matter what value you return - true, false, or the string "I like watermelons." What matters is that you're telling PHP to stop executing the current script at that point. Since you haven't outputted any data (or any redirect headers, or... well, anything), you shouldn't expect to receive any data on the other end.

                            OK i get its not outputting anything now the question is how do I fix it??? I'm not sure what to do and I can't find anything on the internet to help me out besides asking people in forums.

                              From the code you posted up there, the validation page does nothing. It's just the declaration of a class and the exit command.
                              Maybe if you asked it to do something... It's do it? 😉

                                InsideVoid;11008951 wrote:

                                OK i get its not outputting anything now the question is how do I fix it???

                                What do you want it to do instead of outputting nothing?