Jump to content

two && code


johnseito

Recommended Posts

Hi

 

If someone could help me with putting this code together.  It's putting two && code together, I tried many times and it hasn't work for me. 

 

example...

 

If someone enter passwrd, then enter a 2nd repasswrd, and they are not the same, AND if both passwrd and repsswrd are greater and equal to 4 then tell the program to say

 

"Your password does not match.  Password are case sensitive."

 

Thanks.

Link to comment
Share on other sites

<?php
if ($password == $repassword && strlen($password)>= 4){
echo 'password is OK';
} else echo 'bad password';
// or
if ($password != $repassword or strlen($password) < 4){
echo 'bad password';
} else echo 'password is OK';
?>

Link to comment
Share on other sites

Sasa .. thanks .. I need to test two password variables to see if first they are the same... and that both variable

 

"psswrd"  and "repsswrd" are greater and equal to 4.. for the program to say

 

"Your password does not match.  Password are case sensitive."

 

Your code only test one of the variable if they are greater than 4.

 

Looks to me like we need a code :

 

if  "passwrd" NOT equal "repsswrd" and passwrd >= 4 and repasswrd >= 4 then

"Your password does not match.  Password are case sensitive."

end if

 

see if you could put that into php thanks.

Link to comment
Share on other sites

two variable are not the same, they have separate fields.

 

I tried your code and it doesn't work.

 

if  (trim($_POST['psswrd']) != trim($_POST['repsswrd']) or strlen(trim($_POST['psswrd'])) >=4)

 

if both field psswrd, and repsswrd are not blank and also both are 4 characters are more and it gives me

 

Password needs to be at least four characters. Your password does not match. Password are case sensitive.

Link to comment
Share on other sites

YES greater or equal to 4.  The reason is because...

 

two fields for password.. kinda of retype password again.

 

if either password fields,  "psswrd"  and "repsswrd" are blank it will say this field is required.

 

if either of these field is not blank and only if these two field are less than 4 characters it will say

 

"Password needs to be at least four characters"

 

if both of these fields are 4 characters or more and does not equal (is not same) it will say:

 

"Your password does not match.  Password are case sensitive."

 

This last part of the code is what I didn't do because I tried putting two && and the code doesn't work.

 

to your Question again.. yes Greater than or equal to 4

 

thanks

Link to comment
Share on other sites

yea that coding logic is not right..

 

I need something like this:

 

if  "passwrd" NOT equal "repsswrd" and passwrd >= 4 and repasswrd >= 4 then

"Your password does not match.  Password are case sensitive."

end if

 

but I tried putting this into code and it's giving error.

Link to comment
Share on other sites

why you say I need that?  I tried that as you say.. and even if blank fields are enter:

 

the codes say

 

"Your password does not match. Password are case sensitive."

 

that wouldn't be right because password not match because the field is blank.

 

we are going around in a circle.

Link to comment
Share on other sites

Okay, I don't think you understand your own logic. I'm going to put it with generic field names, so you will need to change the field names

 

You want:

"Passwords do not match"

Thus, generically your logic statement would be:

if($_POST['password1'] != $_POST['password2'])
// Passwords didn't match

 

"Password is too short, less than 4 character":

if(strlen($_POST['password1'])<=4)
// Password is too short

 

Putting them together:

if(($_POST['password1'] != $_POST['password2']) || (strlen($_POST['password1'])<=4))
// Password either doesn't match or is too short

 

Now, to explain "if two variables are same they have same length." This is a correct statement. Think about it, if I type in "abcdef" and "abcdef", they are considered to be equal, correct? Thus, they will have the same length. Now, if they don't match, let's say "phil_was_here" and "hello" - they are not equal (passwords don't match), and you won't have to really worry about making sure they are the correct length (since they don't match)

Link to comment
Share on other sites

you forgot that if someone doesn't enter anything in the field, either field and it will tell them that that field is required.  I have this done and it works out correctly. 

if(($_POST['password1'] != $_POST['password2']) || (strlen($_POST['password1'])<=4))

// Password either doesn't match or is too short

 

With your statement of length of first field less than 4.. you are ignoring this no??  you are actually tell the person, password either doesn't match or is too short when they didn't even enter anything.

 

 

I only want it to say "your password doesn't match" when they have both field with length greater or equal to 4.  if either field is less than 4 then it say "Your password need at least 4 characters."

 

and both field with the same length doesn't mean password match.

 

If both password are EQUAL then I don't need to handle it with code and it will just accept it.

 

 

Link to comment
Share on other sites

I feel like you're not even trying to do this on your own. I have given you the separate code snippits, and explained what they are used for. I then showed you how to combine them, to what your original question was. Which I should remind you was:

If someone enter passwrd, then enter a 2nd repasswrd, and they are not the same, AND if both passwrd and repsswrd are greater and equal to 4 then tell the program to say

 

"Your password does not match.  Password are case sensitive."

 

My code does exactly that.

 

Now, I'll give you this pseudocode:

check to make sure passwords match,

    success, if password length is greater than 4

        success, run query

        fail, show error message

    fail, show error message

 

Use the code I gave you, and try to accomplish that.

 

Edit: correction to pseudocode.

Link to comment
Share on other sites

I only want it to say "your password doesn't match" when they have both field with length greater or equal to 4.  if either field is less than 4 then it say "Your password need at least 4 characters."

 

If you want a generic message if any of the conditions are not met, the code posted by others is right, and you need to go back and re-examine what it's doing.

 

If you want it to spit out two different error messages, then you cannot combine those two conditions.  You have to make separate if(...) statements. 

Link to comment
Share on other sites

like this?

 

<?php

if ( $_POST['p1'] == $_POST['p2'] )
{
	if ( ( strlen( $_POST['p1'] >= 4 ) ) && ( strlen( $_POST['p2'] >= 4 ) ) )
		{
			// passwords matched and are both greater than or equal to 4
		}
	else
		{
			$_SESSION['p_error'] = 'Passwords must be at least 4 characters long.';
			header("Location: login.php");
		}
}
else
{
	$_SESSION['p_error'] = 'Passwords don\'t match.';
	header("Location: login.php");
}

?>

Link to comment
Share on other sites

anyone know why this code is giving an error.  I run my page with this code and my form page disappears.

 

if  (trim($_POST['psswrd']) != trim($_POST['repsswrd']) && (strlen(trim($_POST['psswrd']))>=4  && strlen(trim($_POST['repsswrd']))>=4))

 

 

Crayon Violent .. I have separate if statement for checking blanks to either field, and character less than 4 to either field and no blanks, and if fields are greater than 4 but password doesn't match.

 

5kyy8lu3 thank you for your code.. I will take a look at it and try it out.

Link to comment
Share on other sites

Crayon Violent .. I have separate if statement for checking blanks to either field, and character less than 4 to either field and no blanks, and if fields are greater than 4 but password doesn't match.

 

If that's true then why are you writing a fourth if statement to re-check matching passwords and password lengths?

Link to comment
Share on other sites

Are you reading anything we are putting out in front of you?

 

Your code...

if  (trim($_POST['psswrd']) != trim($_POST['repsswrd']) && (strlen(trim($_POST['psswrd']))>=4  && strlen(trim($_POST['repsswrd']))>=4))

... reads:

If the passwords are not equal AND the length of both passwords are greater than 4, throw an error. Also, you don't need to check both passwords for length - it's a waste of resources. If the passwords match, they'll have the same length. If they don't match, you're already throwing an error, so the user will have to re-enter a password anyways.

 

In plain English, type out what are you trying to do.

 

 

Link to comment
Share on other sites

Are you reading anything we are putting out in front of you?

 

Your code...

if  (trim($_POST['psswrd']) != trim($_POST['repsswrd']) && (strlen(trim($_POST['psswrd']))>=4  && strlen(trim($_POST['repsswrd']))>=4))

... reads:

If the passwords are not equal AND the length of both passwords are greater than 4, throw an error. Also, you don't need to check both passwords for length - it's a waste of resources. If the passwords match, they'll have the same length. If they don't match, you're already throwing an error, so the user will have to re-enter a password anyways.

 

In plain English, type out what are you trying to do.

 

Which means the user cannot leave the password field blanks.

Link to comment
Share on other sites

If the passwords are not equal AND the length of both passwords are greater than 4, throw an error.

 

in plain english how does this throw an error??

 

for example if first password is

jhkgfl

 

2nd password is

aefegg

 

 

both are 6 characters both are greater than 4, both password doesn't match.. so how would it throw an error?

 

 

KingP if you don't understand,, than I don't think you should try to help me.  I don't see what you write as make sense.  Your code earlier I already know, and your not helping at all, your are typing again the same thing earlier and I told you why that is not how I wanted it and it's incorrect. 

 

KingP your are providing a simple solution.. my solution is not as simple as you have it.. so I don't need to check both password length is not right.

 

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.