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);
}

Link to comment
Share on other sites

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]).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.