0

So I'm nesting if statements within another to use it for form validation, unfortunately its not working. Say I use an invalid email it just goes to a blank page, which is telling me that its not reading through it. Here's what my code looks like

// Verification

if (empty($name && $username && $email && $pass1 && $pass2))
{
echo "Complete all fields";

// Password match

if ($pass1 <> $pass2)
    {
    echo $passmatch = "Passwords don't match";

    // Email validation

    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
        {
        echo $emailvalid = "Enter a  valid email";

        // Password length

        if (strlen($pass1) <= 6)
            {
            echo $passlength = "Password must be at least 6 characters long";

            // Password numbers

            if (!preg_match("#[0-9]+#", $pass1))
                {
                echo $passnum = "Password must include at least one number!";

                // Password letters

                if (!preg_match("#[a-zA-Z]+#", $pass1))
                    {
                    echo $passletter = "Password must include at least one letter!";
                    }
                }
            }
        }
    }
}

Sorry that the code is a bit messy I'm still working on it. Thanks in advance.

  • 1
    It'd help you plenty just to indent your code. – h2ooooooo Apr 19 '14 at 18:42
  • It should be better now? @h2ooooooo –  Apr 19 '14 at 18:44
  • This is impossible to figure out, we would need to know the rest of the code in order to figure out why it isn't working. As I have no idea what happens after "echo". – Steven Apr 19 '14 at 18:44
  • You've got your braces all tucked up at the end. Follow this convention `if(function){ code }` – Funk Forty Niner Apr 19 '14 at 18:47
  • The message gets displayed after the form is submitted.... @Steven –  Apr 19 '14 at 18:48
  • What would it look like nested then? `if(function){ f(function){ code }code }`? @Fred-ii- –  Apr 19 '14 at 18:49
  • `if(function){ code }` - `if(function){ code }` - `if(function){ code }` etc. however there are exceptions to certain rules. – Funk Forty Niner Apr 19 '14 at 18:49
  • So how would I check if the `if` has been satisfied then continue? Rather then go through the while set of statements? @Fred-ii- –  Apr 19 '14 at 18:51

1 Answers1

0

This won't work like you think it does:

empty($name && $username && $email && $pass1 && $pass2)

You need to use empty on each one.

There's also some other items where you don't need a nested if for. How about this?

// Verification

if (empty($name) || empty($username) || empty($email) || empty($pass1) || empty($pass2))
{
echo "Complete all fields";
// you can stop it here instead of putting the curly brace ALL the way at the bottom :)
return;
}

// Password match
if ($pass1 <> $pass2)
{
    echo $passmatch = "Passwords don't match";
}

// Email validation
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo $emailvalid = "Enter a  valid email";
}

// Password length
if (strlen($pass1) <= 6)
{
    echo $passlength = "Password must be at least 6 characters long";
}

// Password numbers
if (!preg_match("#[0-9]+#", $pass1))
{
    echo $passnum = "Password must include at least one number!";
}

// Password letters
if (!preg_match("#[a-zA-Z]+#", $pass1))
{
    echo $passletter = "Password must include at least one letter!";
}

This way, you can tell the user all the problems in one fell swoop. Why let them make one mistake, re-submit, and then find out they made another mistake they didn't know about?

Joseph
  • 5,070
  • 1
  • 25
  • 26
  • And so this would be secure at a basic level? –  Apr 19 '14 at 19:23
  • @user302975 well, this code is still incomplete. You should probably have a `hasErrors` bool variable and check it before moving on. If any of the errors occur, you shouldn't process anything. Also, you need to be sure to clean the input data from the user before moving on as well. see: http://stackoverflow.com/questions/129677/whats-the-best-method-for-sanitizing-user-input-with-php – Joseph Apr 19 '14 at 20:31