0

When using: if (empty($_POST['Username'])){ .. } The user can input a number of whitespaces so therefore the post is technically not empty, so it continues the insert into my database. I want the fields to contain text.. Is there a way to perform this?

Sophie Mackeral
  • 897
  • 4
  • 12
  • 21
  • 4
    **1)** use `trim()`, **2)** In the HTML part use the attribute `required` **3)** HTML5 even supports the attribute `pattern` to validate the input with regex. – HamZa May 26 '13 at 16:35

2 Answers2

5

Use the trim() function.

if (empty(trim($_POST['Username']))){ .. }
Tom Naessens
  • 1,827
  • 22
  • 38
2

How about;

if (empty($_POST['Username']) || trim($_POST['Username'] == ""){

This will check if the POST key value is empty, but if it is not.. It will remove all whitespaces and then check if it's equal to nothing

Daryl Gill
  • 5,464
  • 9
  • 36
  • 69