Password Combination

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cashflowtips
Forum Newbie
Posts: 22
Joined: Tue Jul 31, 2007 11:06 pm

Password Combination

Post by cashflowtips »

im looking for solution on how to check the password combination...

here is the example: -

Code: Select all

$text = "onlyalphanumericcharacters012345";

if (ereg('[^A-Za-z0-9]', $text)) {
  echo "This contains characters other than letters and numbers";
}
else {
  echo "This contains only letters and numbers";    
}
but the problem here is - i want the code only accept with these kind of combination: -

a) alphabet + number
b) alphabet + symbol
c) number + symbol

can anyone help me? thanks alot
Hemlata
Forum Commoner
Posts: 35
Joined: Mon Sep 10, 2007 5:40 am
Location: India
Contact:

Post by Hemlata »

Hi,

You can get the required results for the combination using following code block...

Code: Select all

// Check the test entered for all the three combinations
$alpha_numeric = ereg('[^A-Za-z0-9]', $text);	// alphanumeric check
$alpha_symbol = ereg('[0-9]', $text);	// symbol+alphabet check
$num_symbol = ereg('[A-Za-z]', $text);	// symbol+number check

// Executes if any match found
if (!$alpha_numeric || !$alpha_symbol || !$num_symbol)
{
	echo "<center><font color='#FF0000'>Entered string is valid :: </font>".$text."</center><br>";
}
// else execute
else
{
	echo "<center><font color='#FF0000'>Invalid text entered ::</font> ".$text;
}
Hope this helps you.. :)
Last edited by Hemlata on Fri Oct 05, 2007 1:02 am, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Password Combination

Post by superdezign »

cashflowtips wrote:a) alphabet + number
b) alphabet + symbol
c) number + symbol
As in you don't accept all three?
And what symbols do you accept? You should be specific.
cashflowtips
Forum Newbie
Posts: 22
Joined: Tue Jul 31, 2007 11:06 pm

Post by cashflowtips »

Hemlata wrote:Hi,

You can get the required results for the combination using following code block...

Code: Select all

// Check the test entered for all the three combinations
$alpha_numeric = ereg('[^A-Za-z0-9]', $text);	// alphanumeric check
$alpha_symbol = ereg('[0-9]', $text);	// symbol+alphabet check
$num_symbol = ereg('[A-Za-z]', $text);	// symbol+number check

// Executes if any match found
if (!$alpha_numeric || !$alpha_symbol || !$num_symbol)
{
	echo "<center><font color='#FF0000'>Entered string is valid :: </font>".$text."</center><br>";
}
// else execute
else
{
	echo "<center><font color='#FF0000'>Invalid text entered ::</font> ".$text;
}
Hope this helps you.. :)
first of all, thanks for the code... maybe my question is not very clear... let me explain once again...

what i really want is the code that can check where the input must have

a) must have Alphabet + must have Number (if the input only one of these two, it will return false)
b) must have Alphabet + must have Symbol (the condition same as A(above))
c) must have Number + must have Symbol (the condition same as A(above))

as for the Symbol, im not sure what is the basic one that usually been used by other website but i know some of them accept symbol as password characters to increase security (hard for hacker to guess the password)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Your question doesn't really make sense to me. Are you saying that the input must have either alphabet + number, OR alphabet + symbol OR number + symbol? Or are you saying that it must meet all of those conditions. Either way, you chose a strange way to ask your question.
cashflowtips
Forum Newbie
Posts: 22
Joined: Tue Jul 31, 2007 11:06 pm

Post by cashflowtips »

The Ninja Space Goat wrote:Your question doesn't really make sense to me. Are you saying that the input must have either alphabet + number, OR alphabet + symbol OR number + symbol? Or are you saying that it must meet all of those conditions. Either way, you chose a strange way to ask your question.

ok. lets put it this way,

i put the condition inside if else statement

Code: Select all

if (alphabet+number)
    echo "Input granted";
else if (alphabet+symbol)
    echo "Input granted";
else if (number+symbol)
    echo "Input granted";
else
    echo "Input denied";
i want the input must be in combination.

example of input:

input : hello
FALSE

input : hello123
TRUE

input : 12345
FALSE

input : 12345@!#
TRUE

this is what i meant by combination
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Again, what is accepted as a symbol? There are a lot of characters in the UNICODE character set.

The smartest way would be to check if there is at least one letter, one number, and one symbol, then add up the boolean results (true = 1, false = 0), then check if that is more than or equal to 2.
cashflowtips
Forum Newbie
Posts: 22
Joined: Tue Jul 31, 2007 11:06 pm

Post by cashflowtips »

superdezign wrote:Again, what is accepted as a symbol? There are a lot of characters in the UNICODE character set.

The smartest way would be to check if there is at least one letter, one number, and one symbol, then add up the boolean results (true = 1, false = 0), then check if that is more than or equal to 2.
mmm... lets take something simple like ~`!@#$%^&*()-_+={}[];:<>?/., would this be alright?
Hemlata
Forum Commoner
Posts: 35
Joined: Mon Sep 10, 2007 5:40 am
Location: India
Contact:

Post by Hemlata »

Hello cashflowtips,

The modified script does your requirement of

Code: Select all

if (alphabet+number)
    echo "Input granted";
else if (alphabet+symbol)
    echo "Input granted";
else if (number+symbol)
    echo "Input granted";
else
    echo "Input denied";
Script ::

Code: Select all

// Check the text entered for all the combinations
$alphabet = ereg('[^a-zA-Z]', $_POST['txtName']);	// alphabet check
$numeric = ereg('[^0-9]', $_POST['txtName']);	// numeric check
$symbol = ereg('[a-zA-Z0-9]', $_POST['txtName']);	// symbol check 
$alpha_numeric = ereg('[^a-zA-Z0-9]', $_POST['txtName']);	// alphanumeric check
$alpha_symbol = ereg('[0-9]', $_POST['txtName']);	// symbol+alphabet check
$num_symbol = ereg('[A-Za-z]', $_POST['txtName']);	// symbol+number check

//executes for alphabet+numeric || symbol+alphabet || symbol+number 
if (($alphabet && $numeric && !$alpha_numeric) || 
	($alphabet && $symbol && !$alpha_symbol) || 
	($symbol && $numeric && !$num_symbol))
{
	echo "Entered string is valid";
}
// else execute
else
{
	echo "Invalid text entered";
}
Also, script will check 'symbol' as anything except an 'alphabet' or a 'number'
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

you can use regex to apply your logic... use preg_match() coupled with your set of regex and you have your asnwer.

hint: first one could be [a-zA-Z]+[0-9]+
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

cashflowtips wrote:mmm... lets take something simple like ~`!@#$%^&*()-_+={}[];:<>?/., would this be alright?
Okay then, from there, why can't you figure it out...?

Code: Select all

if ((preg_match('&[A-Za-z]&', $data) + preg_match('&[0-9]&', $data) + preg_match('&[~`!@#$%^\&*()-_+={}\[\];:<>?/.,]&', $data) >= 2) {
    // Valid
}
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

When using UTF-8 and preg for one project I seem to remember I had to set the mb_internal_coding to UTF-8. Probably not relevant here but as UTF-8 has been mentioned thought I would point it out.
Post Reply