Check Empty Field In Forms

In registration forms, usually you would want to mark certain fields as ‘required’. Thus, users should not be able to continue until these fields is filled. Presented are two methods of checking empty fields in forms:

Using PHP:

<?php
session_start();
unset($_SESSION);
$req_fields = array("_name"=>"name", "timestamp"=>"your time", "field"=>"some field", "check"=>"checkbox");
$msg = "&nbsp;";

function check_empty_fields($method = "post") {
    global $req_fields, $msg;
    $use_method = ($method == "post") ? $_POST : $_GET;
    $errors = "";
    $count_empty = 0;
    foreach ($req_fields as $key => $val) {
        $field_name = (array_key_exists($key, $req_fields)) ?  $req_fields[$key] : $key;
        if (!array_key_exists($key, $use_method) || empty($use_method[$key])) {
            $_SESSION[$key] = true;
            $errors .= "|".$field_name;
            $count_empty++;
        } else {
            $_SESSION[$key] = false;
        }
    }
    if ($count_empty == 0) {
        return true;
    } else {
        $msg = "The following (required) fields are empty:";
        $msg_parts = explode("|", ltrim($errors, "|"));
        $num_parts = count($msg_parts);
        $msg .= "<br><b>";
        for ($i = 0; $i < $num_parts; $i++) {
            $msg .= $msg_parts[$i];
            if ($i <= $num_parts - 2) {
                $msg .= ($i == $num_parts - 2) ? " &amp; " : ", ";
            }
        }
        $msg .= "</b>\n";
        return false;
    }
}
if (isset($_POST['submit'])) check_empty_fields();
// and the form field like:
// echo <input type=\"text" name=\"_name\"";
// if (isset($_POST['_name'])) echo " value=\"".$_POST['_name']."\"";
// if (!$_SESSION['_name']) echo " class=\"invalid\">";
?>

Pros: Pretty. File can be separated.

Cons: Server-side. And I can’t seem to make it work, hahah.

Source: finalwebsites

Using Javascript:

<script language="JavaScript" type="text/javascript">

// Javascript validation functions
// http://www.designplace.org/

//function to check empty fields

function isEmpty(strfield1, strfield2, strfield3) {

//change "field1, field2 and field3" to your field names
strfield1 = document.forms[0].field1.value
strfield2 = document.forms[0].field2.value
strfield3 = document.forms[0].field3.value

  //name field
    if (strfield1 == "" || strfield1 == null || !isNaN(strfield1) || strfield1.charAt(0) == ' ')
    {
    alert("\"Field 1\" is a mandatory field.\nPlease amend and retry.")
    return false;
    }

  //url field
    if (strfield2 == "" || strfield2 == null || strfield2.charAt(0) == ' ')
    {
    alert("\"Field 2\" is a mandatory field.\nPlease amend and retry.")
    return false;
    }

  //title field
    if (strfield3 == "" || strfield3 == null || strfield3.charAt(0) == ' ')
    {
    alert("\"Field 3\" is a mandatory field.\nPlease amend and retry.")
    return false;
    }
    return true;
}

//function to check valid email address
function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  strEmail = document.forms[0].email.value;

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1)
   {
      alert('A valid e-mail address is required.\nPlease amend and retry');
      return false;
    }
    return true;
}

//function that performs all functions, defined in the onsubmit event handler

function check(form)){
if (isEmpty(form.field1)){
  if (isEmpty(form.field2)){
    if (isEmpty(form.field3)){
        if (isValidEmail(form.email)){
          return true;
        }
      }
  }
}
return false;
}

</script>

Pros: Client-side. More importantly, it works. 

Cons: Same long code on every page. And not as pretty T_T

Source: DesignPlace

Leave a Comment