Validation & Error Checking Broken??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philip D Heady

    Validation & Error Checking Broken??

    Hi, I'm validating a simple form for input via post ($PHP_SELF). Near the
    end I check for username and password. I'm using simple if, elseif, else
    statements.

    I require them to enter password twice and check that they match. Problem is
    script doesn't valide past username input and I dont know why!! If you don't
    enter a password it doesn't do the validation anymore, it just dies for some
    reason. I would greatly appreciate anyones help, I've stared at this thing
    for hours and hours.

    Here is code...


    while(list($key , $value) = each($HTTP_GET_ VARS)) {

    echo "$key = $value<br>";

    }


    if ($form && strpos($HTTP_RE FERER, $SCRIPT_NAME) > 0) {


    $cfirstname = clean($cfirstna me);
    $clastname = clean($clastnam e);
    $lname = clean($lname);
    $laddress1 = clean($laddress 1);
    $laddress2 = clean($laddress 2);
    $lcity = clean($lcity);
    $lstate = clean($lstate);
    $lzip = clean($lzip);
    $lcountry = clean($lcountry );
    $lareacode = clean($lareacod e);
    $lprefix = clean($lprefix) ;
    $lsuffix = clean($lsuffix) ;
    $lintcode = clean($lintcode );
    $ldayrate1 = clean($ldayrate 1);
    $ldayrate2 = clean($ldayrate 2);
    $ldescription = clean($ldescrip tion);
    $remail = lower(clean($re mail));
    $username = lower(clean($us ername));
    $password = lower(clean($pa ssword));
    $password2 = lower(clean($pa ssword2));

    if ($username) {

    $q = "SELECT id FROM account WHERE username='". apos($username) ."'";
    $res = mysql_query($q) ;
    $num = mysql_num_rows( $res);

    if ($num > 0) {
    $focus = "username";
    $msg = "This username is taken, please choose a different one.";

    }} elseif ($password != $password2) {
    $focus = "password2" ;
    $msg = "Passwords do not match.";

    } elseif (!$cfirstname) {
    $focus = "cfirstname ";
    $msg = "Please enter your first name.";

    } elseif (!$clastname) {
    $focus = "clastname" ;
    $msg = "Please enter your last name.";

    } elseif (!$lname) {
    $focus = "lname";
    $msg = "Please enter the name of your lodging.";

    } elseif (!$laddress1) {
    $focus = "laddress1" ;
    $msg = "Please enter your lodging's physical address.";

    } elseif (!$lcity) {
    $focus = "lcity";
    $msg = "Please enter your city.";

    } elseif (!$lstate) {
    $focus = "lstate";
    $msg = "Please select/enter your state or province.";

    } elseif (!$lzip) {
    $focus = "lzip";
    $msg = "Please enter your postal code.";

    } elseif (!$lcountry) {
    $focus = "lcountry";
    $msg = "Please enter your country.";

    } elseif (!$rphone) {
    $focus = "rphone";
    $msg = "Please enter your reservations phone number";

    } elseif (!$ldayrate1) {
    $focus = "ldayrate1" ;
    $msg = "Please enter your minimum daily rate.";

    } elseif (!$ldayrate2) {
    $focus = "ldayrate2" ;
    $msg = "Please enter your maximum daily rate.";

    } elseif (!$ldescription ) {
    $focus = "ldescripti on";
    $msg = "Please provide a description of your lodging.";

    } elseif (strlen($ldescr iption) > 200) {
    $focus = "ldescripti on";
    $msg = "Your description is limited to 200 characters.";

    } elseif (!is_valid_emai l($remail)) {
    $focus = "remail";
    $msg = "Please enter a valid email address.";

    } elseif (!$username) {
    $focus = "username";
    $msg = "Please enter a username.";

    } elseif (!$password) {
    $focus = "password";
    $msg = "Please enter a password.";

    } elseif (!$password2) {
    $focus = "password2" ;
    $msg = "Please re-enter your password.";

    } else { etc, etc..


  • steven mestdagh

    #2
    Re: Validation &amp; Error Checking Broken??

    Philip D Heady <pdheady@comcas t.net> wrote:[color=blue]
    > Hi, I'm validating a simple form for input via post ($PHP_SELF). Near the
    > end I check for username and password. I'm using simple if, elseif, else
    > statements.
    >
    > I require them to enter password twice and check that they match. Problem is
    > script doesn't valide past username input and I dont know why!! If you don't
    > enter a password it doesn't do the validation anymore, it just dies for some
    > reason. I would greatly appreciate anyones help, I've stared at this thing
    > for hours and hours.
    > ...
    > if ($username) {
    >
    > $q = "SELECT id FROM account WHERE username='". apos($username) ."'";
    > $res = mysql_query($q) ;
    > $num = mysql_num_rows( $res);
    > if ($num > 0) {
    > $focus = "username";
    > $msg = "This username is taken, please choose a different one.";
    >
    > }} elseif ($password != $password2) {[/color]
    ^^^^^^^^^^^^^^^ ^^^^^^^^
    this is your problem, i think. probably you are testing unset variables
    with the != test.
    how about first testing for emptiness, and then checking whether your
    passwords match? something like:

    if (!$username || empty($username )) {
    $focus = "username";
    $msg = "Please enter a username.";
    } elseif (!$password || empty($password )) {
    $focus = "password";
    $msg = "Please enter a password.";
    } elseif (!$password2 || empty($password 2)) {
    $focus = "password2" ;
    $msg = "Please re-enter your password.";
    } elseif ($password != $password2) {
    $focus = "password2" ;
    $msg = "Passwords do not match.";
    } elseif (
    ...
    ...
    } else {
    $q = "SELECT id FROM account WHERE username='". apos($username) ."'";
    $res = mysql_query($q) ;
    $num = mysql_num_rows( $res);
    if ($num > 0) {
    $focus = "username";
    $msg = "This username is taken, please choose a different one.";
    }
    }

    regards,
    steven.

    Comment

    • Philip D Heady

      #3
      Re: Validation &amp; Error Checking Broken??

      Yep, moving the username error checking to the bottom fixed problem. I also
      made use of empty() function. Thanks.

      Philip D. Heady



      Comment

      Working...