-1

I need to know how to verify that the user entered empty string in a sentence if in a string with multiple spaces in blank

Example

  "                         "

if user entered a emṕty string, the program must show an alert as this

echo "The username must not be empty";
NikiC
  • 100,734
  • 37
  • 191
  • 225
  • 2
    PHP is server side, JavScript/jQuery is client side. You're mixing the two, it'd be easier to just do this in jQuery OR PHP depending on your needs – Andy Holmes Jan 26 '15 at 16:46
  • "alert" is a js function.... – Eugen Jan 26 '15 at 16:46
  • if you want to stop or validate form submission, you can prevent it with js, like here http://stackoverflow.com/questions/8664486/javascript-to-stop-form-submission – Eugen Jan 26 '15 at 16:49

2 Answers2

8

Use trim to remove whitespace from a var...

$name = trim($_GET['name']);

if ($name == '') //empty
fire
  • 21,383
  • 17
  • 79
  • 114
  • Out of interest, what's the difference between this and `!$name`? – Andy Holmes Jan 26 '15 at 16:48
  • 1
    @AndyHolmes with PHP being loosely typed; nothing really. `!$name` is equivalent to `$name == false` which is equivalent to `$name == ''` ... it would only make a difference with strict evaluation `===`; you do have to be a little careful though as `0 == false` but `'0' == true` ;) – CD001 Jan 26 '15 at 16:52
2

Try this

if (strlen(trim($yourString)) == 0) {
    // Do something
}

If the length of the string is 0 after the spaces are trimmed/removed.

KWILLIAMS
  • 193
  • 3
  • 10