Jump to content


Photo

Form Checking (Missing Inputs)


  • Please log in to reply
2 replies to this topic

#1 artemisbow

artemisbow
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 06 May 2006 - 04:48 AM

Hi, I'm using the following code to check that the form has been filled up completely
if((!value1) || (!value2)... || (!valueX)){
  echo "Some fields have not been filled in";
  }

However, when a user enters '0', I want the script to know that it is not empty, but neither the above method nor using empty() works. What should I do instead? Thanks!


#2 prodynetech

prodynetech
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 06 May 2006 - 05:16 AM

You could use regular expressions.
function isValidInput( $str )
{
    // Test for a zero, null, or empty string
    if( !ereg( "^[a-zA-Z]$", $str ) || empty( $str ) || ! isset( $str ) || ( $str < 1 ) )
        return false;    
}

Now you can get creative and check for ranges or specific patterns. For example postal codes:
$pcode=str_replace(" ","",$in_post_code);
if (!ereg('^[a-zA-Z]{1,2}[0-9]{1,2}[a-zA-Z]{0,1}[0-9]{1}[a-zA-Z]{2}$', $pcode))
{
   return false;
}

Check out www.php.net for additional information on php functions that might be helpful to you.

Regards,

ProdyneTech

[!--quoteo(post=371765:date=May 6 2006, 12:48 AM:name=artemisbow)--][div class=\'quotetop\']QUOTE(artemisbow @ May 6 2006, 12:48 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi, I'm using the following code to check that the form has been filled up completely
if((!value1) || (!value2)... || (!valueX)){
  echo "Some fields have not been filled in";
  }

However, when a user enters '0', I want the script to know that it is not empty, but neither the above method nor using empty() works. What should I do instead? Thanks!
[/quote]


#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 May 2006 - 05:20 AM

What you need to do is actually test each field to make sure that something was entered:
<?php
$errs = array();
foreach ($_POST as $key => $val)
    if ($key != 'submit')  // I'm assuming that the submit button in your form is named 'submit'
       if (strlen(trim(stripslashes($val))) == 0) // Is the field truly empty  -- strlen is 0
             $errs[] = $k;
if (!empty($errs)) // if the $errs array is not empty, at least one field was left blank
   echo 'The following fields were left blank:<br><span style="color:red">' . implode('<br>',$errs) . '</span>';
?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users