Solved

eregi pswd Checker

Posted on 2006-07-17
11
620 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

825 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