Solved

eregi pswd Checker

Posted on 2006-07-17
11
621 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
Technology Partners: 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php error 27 56
PHP Curl Problem 10 36
How do i use the await event in php echo 5 18
Echo values after a query in php 5 22
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

756 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