1

When using PHP how can I validate a simple text input to check if the field only contains space character, I want to prevent it from being possible to sign up to my website with a blank name consisting of just spaces and the same goes for other inputs such as comments? If I check if it is empty it just returns it as not empty as it considers a space as a character regardless.

For future reference the answer was to first trim my post data:

$variable = mysql_real_escape_string(stripslashes(trim($_POST['field'])));

Also sanitised it here and then as for validation, you can then just use empty():

if(empty($variable)) {
//do something
}
2
  • 4
    trim Commented Jul 23, 2013 at 14:04
  • I would recommend you defer escaping anything until the last possible moment; rather, letting PDO or similar technology handle it for you. This would allow you to work with and test the raw data submit by the user before sending it off for storage. Commented Jul 23, 2013 at 15:32

2 Answers 2

5

trim() will remove leading/trailing whitespace, as per the docs:

Strip whitespace (or other characters) from the beginning and end of a string

As discussed already, empty() will return a boolean (and needs a variable argument pre-5.5), indicating whether the passed variable is "empty" or not.

However, this may lead to some peculiar behavior. Consider the following:

$value = "0";            // possibly a perfectly valid non-empty value *
var_dump(empty($value)); // bool(true) ... what?

* I could foresee this: "How many times have I been arrested for public indecency? Um, well 0... Invalid input!? How does it know!?"

PHP will evaluate a string of only "0" as empty:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

Your best bet is to test the trimmed string against an empty one via identical-equality (which tests the type too):

if (trim($value) !== '') {
    // the string wasn't empty
    // after calling trim()
}

The empty(0) issue is an edge case, but avoiding it will potentially save you from tearing your hair out.

Sign up to request clarification or add additional context in comments.

Comments

4

You can trim the posted input and see if its empty, if it is, display an error.

if(empty(trim($_POST['comments'])))
{
    // Its empty so throw a validation error
    echo 'Input is empty!'; 
}
else
{
    // Input has some text and is not empty.. process accordingly.. 
}

More info on trim() can be found here: http://php.net/manual/en/function.trim.php

6 Comments

To have more advanced checking you can read into "Regular Expressions". For example a regular expression of "[a-zA-Z0-9_\-]{3,20}" would only allow to have names that have alphanumeric characters or "_" or "-" and has to be minimum 3 characters and maximum of 20. For example you can start here: regular-expressions.info
I've already tried that and assumed it was deprecated or something as I receive this error: Fatal error: Can't use function return value in write context in *** on line 19
regular-expressions is overkill for this purpose.
I doubt trim will ever be deprecated, it's like trying to deprecate "echo" or "var_dump". That said, paste line 19, it seems that you're using trim wrong.
Ah you need to trim it first before using it in the if statement accompanying empty(), I used trim on my variable that was set of the form data, rather than directly onto the $_POST data, which I should have done.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.