johnseito Posted February 9, 2009 Share Posted February 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/ Share on other sites More sharing options...
sasa Posted February 9, 2009 Share Posted February 9, 2009 <?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'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-757867 Share on other sites More sharing options...
johnseito Posted February 9, 2009 Author Share Posted February 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-757876 Share on other sites More sharing options...
genericnumber1 Posted February 9, 2009 Share Posted February 9, 2009 Why don't YOU put it into PHP and try it out, because you're wrong and sasa's post is correct. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-757882 Share on other sites More sharing options...
sasa Posted February 9, 2009 Share Posted February 9, 2009 if two variables are same they have same length Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-757883 Share on other sites More sharing options...
johnseito Posted February 9, 2009 Author Share Posted February 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-757887 Share on other sites More sharing options...
johnseito Posted February 9, 2009 Author Share Posted February 9, 2009 Why don't YOU put it into PHP and try it out, because you're wrong and sasa's post is correct. How you know I am wrong? You don't even know how I want my logic to be. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-757891 Share on other sites More sharing options...
Philip Posted February 9, 2009 Share Posted February 9, 2009 >=4 Read's "greater than or equal to 4". Think you want it the other way <=4 Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-757892 Share on other sites More sharing options...
johnseito Posted February 10, 2009 Author Share Posted February 10, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-758581 Share on other sites More sharing options...
Philip Posted February 10, 2009 Share Posted February 10, 2009 So, this reads: if (trim($_POST['psswrd']) != trim($_POST['repsswrd']) or strlen(trim($_POST['psswrd'])) >=4) If the trimmed passwords don't match or the length of the password is greater than or equal to 4... Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-758601 Share on other sites More sharing options...
johnseito Posted February 10, 2009 Author Share Posted February 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-758605 Share on other sites More sharing options...
Philip Posted February 10, 2009 Share Posted February 10, 2009 You need this: if((trim($_POST['psswrd']) != trim($_POST['repsswrd'])) || strlen(trim($_POST['psswrd'])) <=4) Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-758607 Share on other sites More sharing options...
johnseito Posted February 10, 2009 Author Share Posted February 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-758614 Share on other sites More sharing options...
Philip Posted February 10, 2009 Share Posted February 10, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-758627 Share on other sites More sharing options...
johnseito Posted February 10, 2009 Author Share Posted February 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-758645 Share on other sites More sharing options...
Philip Posted February 10, 2009 Share Posted February 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-758663 Share on other sites More sharing options...
.josh Posted February 10, 2009 Share Posted February 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-758685 Share on other sites More sharing options...
5kyy8lu3 Posted February 10, 2009 Share Posted February 10, 2009 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"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-758703 Share on other sites More sharing options...
johnseito Posted February 11, 2009 Author Share Posted February 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-759365 Share on other sites More sharing options...
gevans Posted February 11, 2009 Share Posted February 11, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-759375 Share on other sites More sharing options...
johnseito Posted February 11, 2009 Author Share Posted February 11, 2009 If that's true then why are you writing a fourth if statement to re-check matching passwords and password lengths? except one of the if statement, the one I listed on top, I have problem with. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-759383 Share on other sites More sharing options...
Philip Posted February 11, 2009 Share Posted February 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-759395 Share on other sites More sharing options...
Solar Posted February 11, 2009 Share Posted February 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-759402 Share on other sites More sharing options...
johnseito Posted February 11, 2009 Author Share Posted February 11, 2009 Which means the user cannot leave the password field blanks. if(($_POST['password1'] != $_POST['password2']) || (strlen($_POST['password1'])<=4)) with KingP's code earlier they could leave a field blank. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-759408 Share on other sites More sharing options...
johnseito Posted February 11, 2009 Author Share Posted February 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144425-two-code/#findComment-759413 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.