Jump to content

Form validation - Zero "0" not being accepted


Eiolon

Recommended Posts

I have a field that asks for a number.  Any number is being accepted except for zero.  It will accept double zeros but not a single zero.

 

Here is my validation:

 

if (empty($_POST['seats'])) {
$errors[] = 'Enter the number of seats available';
}

if (!ctype_digit($_POST['seats'])) {
$errors[] = 'The number of seats must be a digit';
}

if ($_POST['seats']) {
$s = escape_data($_POST['seats']);
}


 

My escape data function

 

function escape_data ($data) {
global $dbc;
if (ini_get('magic_quotes_gpc')) {
	$data = stripslashes($data);
}
return mysql_real_escape_string (htmlspecialchars(trim(strip_tags($data))), $dbc);
}

The string "0" is considered as empty (i.e. empty("0") returns TRUE).  In the first if statement you could either explicitly cater for "0" or use some other method to check on the string being really empty (e.g. strlen to check the length of the string [in bytes]).

Turn the data into a string with:

$num = "$_POST['num']"; // note the quotes

Then

if ($num != NULL)

later if you compare your string against an interger PHP will automatically treat the string as an integer.

 

 

HTH

Teamatomic

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.