Solved

eregi pswd Checker

Posted on 2006-07-17
11
618 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 37

Expert Comment

by:Harisha M G
Comment Utility
Hey Autogard :)

Great minds think alike ;-)
0
 
LVL 8

Expert Comment

by:Autogard
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:jezella
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:jezella
Comment Utility
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
Comment Utility
Got it.  Thank you to you both.  I now have a much better understand on how this works.
Cheers.

Jezella
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 …

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now