Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Ive been following a tutorial on phpacademy and all has gone well so far but i've come to validating a register form and have ran into a problem.

The problem: when I test the validation on all fields the password statement (shown below) is shown regardless of whether the password success criteria is met or not. For example, if the password IS the correct amount of characters but the username IS NOT, the error message for the password IF function is displayed and not the error message for the username, meaning I cannot get the form the display "Success!".

Below is my PHP code for the form:

PHP
$submit = $_POST['submit'];
$fullname = strip_tags($_POST['fullname']);
$username = strip_tags($_POST['username']);
$emailaddress = strip_tags($_POST['emailaddress']);
$repeatemailaddress = strip_tags($_POST['repeatemailaddress']);
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = date("Y-m-d");

if ($submit)
{	
	//check for existance
	if ($fullname&&$username&&$emailaddress&&$repeatemailaddress&&$password&&$repeatpassword)
	{
		//encrypt password
		$password = md5($password);
		$repeatpassword = md5($repeatpassword);
		
		if ($password=$repeatpassword&&$emailaddress=$repeatemailaddress)
			{
			
			//check character length of username and fullname
			if (strlen($username)>25||strlen($fullname)>25)
				{
					echo "Length of username or fullname is too long!";
				}
				else
					{
					//check email length
						if (strlen($emailaddress)>50||strlen($emailaddress)<10)
						{
							echo "Your email address must be between 10 and 50 characters!";
						}
							else
							{
							//check password length
								if (strlen($password)>25||strlen($password)<6)
								{
									echo "Your password must be between 6 and 25 characters!";
								}			
									else
								{
								//register the user	
								echo "Success!";
								}
							}
					}
				
				}
		else
			echo "Your passwords or email address do not match!";
	
	} //wnd of password/email check
else
	echo "Please fill in all fields!";
	
}
Posted
Comments
vbmike 5-Jul-14 11:29am    
Double check all the brackets for the if statements. It looks like you are missing some on the last two else entries....cursory check in editor reveals all the if statements are not matching up correctly, that is if I am reading them correctly.

Should this line:
if ($password=$repeatpassword&&$emailaddress=$repeatemailaddress)


not be:
if ($password==$repeatpassword&&$emailaddress==$repeatemailaddress)
 
Share this answer
 
Comments
jba1991 7-Jul-14 13:24pm    
I think you may be right there David, but still no success! I dont have a clue what is going on, I added the condition brackets ({}) for the 'else's' i missed as well but its doing the same thing
I think I have found the solution.

Because an md5 encrypted password is always 32 characters and my validation says no more than 25 characters, it was always validating my password as 32 characters once encrypted, which means the length of the users chosen password was irrelevant.

I changed the max character length to 36 and it has started working.

So for anyone wondering, (as far as i know) md5 encrypted passwords need a max length of 32+ when validating the character length!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900