多个错误的表单验证

What would be the best way to code for the following

  1. To check if its empty
  2. That its alpha
  3. Length

I am wanting a way that I am able to combine the following if statements

Current Code

if (isset($_POST['submitButton'])) { 

    $fullName      = $_POST['fullname'];

    if(fullName != ' ')
      {
        $errorfullName .= 'Please Enter Your Name';
      }

      }
    }

if statements that need to be included:

if (!ctype_alpha(str_replace(array("'", "-"), "",$fullName))) { 
            $errorfullName .= '<span class="errorfullName">*First name should be alpha characters only.</span>';
}

if (strlen($fullName) < 3 OR strlen($fullName) > 40) {
            $errorfullName .= '<span class="errorfullName">*First name should be within 3-40 characters long.</span>';
}

Your are missing $ sign before fullName.Use empty function to check weather the string is empty or not. Use the below code

  if (isset($_POST['submitButton'])) { 

        $fullName      = $_POST['fullname'];

        if(empty($fullName))
          {
            $errorfullName .= 'Please Enter Your Name';
          }

          }

If you need to combine more statements, you can do it with ( if{} elseif{} else{/*NO ERROR*/}. But I think there is a smarter solution:

function valide_fulname($fullname) {
    if (isset($fullName) && trim($fullName)!='')
      return 'Please enter your name.';
    if (!ctype_alpha(str_replace(["'", "-"], "", $fullName)))
      return 'First name should be alpha characters only.';
    if (strlen($fullName)<3 || strlen($fullName)>40)
      return 'First name should be within 3-40 characters long.';
    // no error
    return false;
}

if (isset($_POST['submitButton'])) {
  $error = valide_fullname($_POST['fullname']);
  if (!$error) 
      echo "It's OK!";
  else 
      echo '<span class="errorfullName">' . $error . '</span>';  
}