Universal empty field validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Universal empty field validation

Post by Ree »

Is there a universal way to check if the submited field was empty? empty($_POST['field']) isn't universal, as it misses some values, for example, '0'.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

but a variable being equal to 0 could be significant:-D
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Code: Select all

if(!isset($_POST['field']) || $_POST[field] == '')
May work in many circumstances when testing a single field - aren't POST integers passed as strings initially? Hence using is_numeric() rather than is_int() to check for numbers? I think so...
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

another option is:

Code: Select all

if ((!$first_name) || (!$last_name)) {

echo "Ops you missed required fields";

} else {

continue with script

}

hth
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Except you'd need to enable register_globals unless you intend them to reflect the full POST superglobal reference?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

<?php

if (isset($_POST['var']) && $_POST['var'] != '') {

  //do it..

}
else {

  //don't do it..

}

?>
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

function check_field($value)
{
$value = trim($value);
return (strlen($value) > 0) ? true : false;
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What I like to do in my applications have 3 claseses have 3 classes for the following:
a) class.validate.php (contains all the validation methods, such as isPostalCode, isAllLetters, whatever your needs may be) (this is a static class)
b) class.checkvalidate.php
I pass 2 things to this function, an array of expected values and validation type (see class.validate.php) and the input method ($_GET, $_POST, $_COOKIE, whatever. Although this does not need to be an actual input type, you can pass anything you need validated to this class.
c) class.erroroutput.php
This will be called during the checkvalidate process which will build the appropriate errors into an array. For example, it will sort all the "missing variables, and all the invalid variables

In the end it looks something along the lines of

Code: Select all

$valid = array('username' => 'isAllLetters', 'email' => 'isEmail');
$validation = new checkvalidate();

if ($validation->isValid($valid, $_POST)) {
    $errors = $validation->getErrors();
}
else {
    //everyone is valid.. 
}
Post Reply