Solved

eregi pswd Checker

Posted on 2006-07-17
11
622 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

734 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