Universal empty field validation
Moderator: General Moderators
Universal empty field validation
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
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
Code: Select all
if(!isset($_POST['field']) || $_POST[field] == '')Hi,
another option is:
hth
another option is:
Code: Select all
if ((!$first_name) || (!$last_name)) {
echo "Ops you missed required fields";
} else {
continue with script
}hth
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
Code: Select all
<?php
if (isset($_POST['var']) && $_POST['var'] != '') {
//do it..
}
else {
//don't do it..
}
?>Code: Select all
function check_field($value)
{
$value = trim($value);
return (strlen($value) > 0) ? true : false;
}- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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
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..
}