Solved

eregi pswd Checker

Posted on 2006-07-17
11
623 Views
Last Modified: 2012-05-05
Hi experts.  I'm still learning so here is the question.

Using either eregi or ereg I wish to create a password checker.  The password MUST contain both uppercase and lowercase characters and also contain numbers.

So aHli763 would be valid but AHLI763 would not.

<?php

$pswd = 'AJhkijh475';


if (!ereg("^[a-zA-Z0-9]{8,12}$",$pswd)) {
      echo "The password must consist letters of upper and lowecase,
      it must also contain numerals and must be between 8 - 12
      characters in length.";
}

echo 'Passed';

?>

Many thanks all.

Jezella
0
Comment
Question by:jezella
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
11 Comments
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17125988
Hi,

if(preg_match("/([a-z][A-Z]|[A-Z][a-z])/",$pswd))
    echo "Password OK"
else
    echo "The password must consist letters of upper and lowecase"


---
Harish
0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 250 total points
ID: 17126006
sorry.. left semicolon and other conditinos...

if( preg_match("/[a-z]/",$pswd)
 & preg_match("/[A-Z]/",$pswd)
 & preg_match("/\d/",$pswd)
 & (strlen($pswd)>7)
 & (strlen($pswd) < 13)
)
    echo "Password OK";
else
    echo "The password must consist letters of upper and lowecase";

0
 
LVL 8

Assisted Solution

by:Autogard
Autogard earned 250 total points
ID: 17126011
Kind of clunky, but...

if(!((ereg("[a-z]",$pswd) and (ereg("[A-Z]",$pswd) and (ereg("[0-9]",$pswd) and strlen($pswd) >= 8 and strlen($pswd) <= 12))
{
 ...
}
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 37

Expert Comment

by:Harisha M G
ID: 17126017
Hey Autogard :)

Great minds think alike ;-)
0
 
LVL 8

Expert Comment

by:Autogard
ID: 17126046
Haha.  Yup!  Unfortunately I must have been like 2 seconds slower in posting.  And it's all because I was looking at your first answer and thinking "hmm, it looks like he's missing some conditions and the length".  And then I finally got around to posting and... BAM... there you have it with conditions and length and all!  Bah!  :-)
0
 

Author Comment

by:jezella
ID: 17126145
mgharish and Autogard thank you.

I tried both as follows.

<?php

$pswd = 'AbhCC545j';


if(!((ereg("[a-z]",$pswd) and (ereg("[A-Z]",$pswd)))) and (ereg("[0-9]",$pswd) and strlen($pswd) >= 8 and strlen($pswd) <= 12))
{
 echo 'works'
;}

else echo 'Failed';


if( preg_match("/[a-z]/",$pswd)
 & preg_match("/[A-Z]/",$pswd)
 & preg_match("/\d/",$pswd)
 & (strlen($pswd)>7)
 & (strlen($pswd) < 13)
)
    echo "Password OK";
else
    echo "The password must consist letters of upper and lowecase";

?>

In the case of the first example a slight problems seems to exist with the parenting.  Can't see it though.  I am try to getter my understanding of ereg etc a bit better.  Got to leave the office for a short time

Jezella
0
 
LVL 8

Expert Comment

by:Autogard
ID: 17126197
You put in a whole bunch of extra parentheses.  If you leave it as:

if(
    !
    (
        ereg("[a-z]",$pswd) and
        ereg("[A-Z]",$pswd) and
        ereg("[0-9]",$pswd) and
        strlen($pswd) >= 8 and
        strlen($pswd) <= 12)
    )
{
 echo 'works';
}

...you should be fine.
0
 
LVL 8

Expert Comment

by:Autogard
ID: 17126200
Also, I should note that the only reason why I used "ereg" was because that is what you mentioned in the question.  I actually prefer "preg_match" like mgh_mgharish used and you might want to consider using that as well.
0
 
LVL 8

Expert Comment

by:Autogard
ID: 17126205
0
 

Author Comment

by:jezella
ID: 17126304
Mgharish, your example works fine and is much appreciated.  When I mentioned the first example, I was refering to my reply post.  It seems that the example of Autogard has a fault with the parenting.  Sorted it to some extent.  Hope we do not cross paths in sending here.  Sorry, in and out of the office for a bit.

Jezella

0
 

Author Comment

by:jezella
ID: 17127123
Got it.  Thank you to you both.  I now have a much better understand on how this works.
Cheers.

Jezella
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question