PHP name validation working in reverse

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jovankau
Forum Newbie
Posts: 10
Joined: Tue Feb 26, 2008 12:25 am

PHP name validation working in reverse

Post by jovankau »

Hi,

I have modified some PHP code to validate name entry in a web form. It is working, just opposite to how it should i.e. allow blank entries and doesn't allow entries with names.

Any suggestions on how to resolve this would be greatly appreciated.

Here is is:

Code: Select all

 
 
  // Validate the name
  
  if (isset($_REQUEST['name']) && !empty($_REQUEST['name'])) 
 
{
    /* First lets set up a string without spaces to make this all easier to read */
    $name = trim($_REQUEST['name']);
 
      // the user's name cannot be a null string
      $errors[] = "You must supply a name.";
 
    if (strlen($formVars['name']) > 30) {
      $errors[] =
        "The name can be no longer than 30 characters";
 
}}
 
Thanks,
Jo
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP name validation working in reverse

Post by Christopher »

You should setup a test script to play with PHP's if() statement. Try out the various logic combinations to see how it works.

Code: Select all

// $myvar = '';
// $myvar = 0;
// $myvar = 'foo';
if (isset($myvar) && !empty($myvar)) { echo "isset && !empty"; )
if (isset($myvar) || empty($myvar)) { echo "isset || empty"; )
if (!isset($myvar) && !empty($myvar)) { echo "!isset && empty"; )
if (!isset($myvar) || !empty($myvar)) { echo "!isset || !empty"; )
// etc...
 
(#10850)
jovankau
Forum Newbie
Posts: 10
Joined: Tue Feb 26, 2008 12:25 am

Re: PHP name validation working in reverse

Post by jovankau »

Hi Chris,

Have given you suggestions a go. Now I am getting the "you must supply a name" error when a name has been and when it hasn't!

Here is one example of the code:

Code: Select all

 
 if (isset($_REQUEST['name']) && !empty($_REQUEST['name'])) echo "isset && empty";
  
 
{
    /* First lets set up a string without spaces to make this all easier to read */
    $name = trim($_REQUEST['name']);
 
      // the user's name cannot be a null string
      $errors[] = "You must supply a name.";
 
    if (strlen($formVars['name']) > 20) {
      $errors[] =
        "The city can be no longer than 20 characters";
 
}}
 
Do you have any other suggestions?

Thanks,
Jo
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: PHP name validation working in reverse

Post by superdezign »

The braces aren't connected to the if statement. They are just stray braces. The if statement's block is ended by the echo statement.
jovankau
Forum Newbie
Posts: 10
Joined: Tue Feb 26, 2008 12:25 am

Re: PHP name validation working in reverse

Post by jovankau »

Thanks for your help. It has done the trick!

Sorry to ask such basic questions, not even classified as a 'newbie' yet!!

Have a great day,
Jo
Post Reply