Jump to content

Form validation problems


TrueColors

Recommended Posts

I have Gender, Date of Birth, and Captcha validations. Though they are not working.

 

Lets start with gender. Then captcha, then DOB.

 

I call the Gender check via doing if(user::isValidGender($this->gender) == FALSE) $errors[] = "Invalid gender";

"user" is another class.

 

The function isValidGender() is as follows

    public function isValidGender($gender)
    {
        $validGenders = array("male", "female", "mtf", "ftm", "androgyne", "intersex");
            
        if(in_array($gender, $validGenders) == FALSE) return false;
    }

 

When I do if(in_array($gender, $validGenders) == FALSE) die("error"); it doesn't die. It displays "Invalid Gender" which means that if it doesn't do the die() it returns true. Though it checks if the function returns false when calling "isValidGender()".

 

If I put the die() above the if, it dies. So it obviously is returning true, but why is it displaying the error?

Link to comment
Share on other sites

This will now return true if the given gender is valid, or false otherwise.

 

public function isValidGender($gender)
{
    $validGenders = array("male", "female", "mtf", "ftm", "androgyne", "intersex");
    return in_array($gender, $validGenders);
}

Link to comment
Share on other sites

Thanks thorpe that worked! Can you explain why my original method didn't work?

 

- My next problem is the captcha.

md5($captcha) is 706a335b65ce9ccabd6c683b2dec13d2

$_SESSION['ckey'] is 706a335b65ce9ccabd6c683b2dec13d2

(I echo'd them both out and they are exact)

 

I call the function by doing this if(user::isValidCaptcha($this->captcha) == FALSE) $errors[] = "Your captcha image was incorrect!";

The function is

public function isValidCaptcha($captcha)
{
	die(md5($captcha) . '<br />' . $_SESSION['ckey']); // This was to test and both results were the same

	if(md5($captcha) != $_SESSION['ckey']) return false;
}

 

Even though both are the same, why does it display the error that it's incorrect?

Link to comment
Share on other sites

return ((md5($captcha) == $_SESSION['ckey']) ? true : false);

That method works.

 

Again I ask, along with my previous issue: Why did it not work the way I had coded it?

 

My next issue, which is slightly different. Is for DOB. I call it the same way and this is the function

	public function isValidDOB($day, $month, $year)
{
	if(($day > 31 || $day < 01) || ($month > 12 || $month < 1) || ($year >= date('Y', time()) || $year <= 1910))
	{
		return false;	
	}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.