[SOLVED]Regex to check "at least one" containment

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

[SOLVED]Regex to check "at least one" containment

Post by novice4eva »

Hi friends, i have been trying to validate a password field, the criteria being that it must contain at-least one special character and at-least one numeric value. All i could come up with is

Code: Select all

regex = /[a-zA-Z]+[0-9]+[\W]+/
which works fine for codes like xyz123#$ but this will obviously not work for something like #$asd12asd....Is there a way to do this or so i have to do 3 individual checks like

Code: Select all

 
legalChars=/[a-zA-Z]+/;
if(legalChars.test(passwordString))
{
legalChars=/[0-9]+/;
if(legalChars.test(passwordString))
{
legalChars=/[\W]+/;
if(legalChars.test(passwordString))
    return true;
}
}
 
Thanks in advance


EDIT : I at the moment did

Code: Select all

 
                $valid = false;
                $legalChars = array("([a-zA-Z]+)","([0-9]+)","([^a-zA-Z0-9]+)"); // allow only letters and numbers
                if(ereg($legalChars[2],$this->getProperty($theField)))
                    echo 'valid';
                if(ereg($legalChars[0],$this->getProperty($theField))!==false && ereg($legalChars[1],$this->getProperty($theField))!==false && ereg($legalChars[2],$this->getProperty($theField))!==false)
                    $valid = true;
 
                if(!$valid)
                {
                     $s = "The field \"".$s."\" must contain atleast 1 number,1 special character and 1 alphabet.";
                    return $this->warnInvalid($theField,$s);
                }
 
 
It works, but i had previously done

Code: Select all

$legalChars = array("([a-zA-Z]+)","([0-9]+)","([\W]+)");
that "[\W]+ " didn't work for me!! :dubious:
Last edited by novice4eva on Mon Jan 05, 2009 11:22 pm, edited 1 time in total.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex to check "at least one" containment

Post by prometheuzz »

novice4eva wrote:Hi friends, i have been trying to validate a password field, the criteria being that it must contain at-least one special character and at-least one numeric value. ...
This will do the trick:

Code: Select all

if(preg_match('/^(?=.*?\d)(?=.*?[~!@#$%^&*()]).*$/', $password)) {
  // ok
}
of course, you will have to determine what a special character is: just remove or add them from the character class: [~!@#$%^&*()] (just leave the [ and ] in place!)
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Regex to check "at least one" containment

Post by novice4eva »

Thanks a lot, that works great. Humm my next task would be to break that regex and understand what it's really doing, regex has never been my forte :google: ...

again thanks A LOT :drunk:
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex to check "at least one" containment

Post by prometheuzz »

novice4eva wrote:Thanks a lot, that works great. Humm my next task would be to break that regex and understand what it's really doing, regex has never been my forte :google: ...

again thanks A LOT :drunk:
No problem.

And here's a short explanation:

Code: Select all

^                  # the start of the string
(?=                # start positive look ahead
  .*?              #   zero ore more characters of any type
  \d               #   match a single digit
)                  # start positive look ahead
(?=                # start positive look ahead
  .*?              #   zero ore more characters of any type
  [~!@#$%^&*()]    #   match a single special character
)                  # start positive look ahead
.*                 # "consume" the entire string
$                  # the end of the string
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Regex to check "at least one" containment

Post by novice4eva »

:D THANKS THANKS THANKS a trillion times :bow:
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex to check "at least one" containment

Post by prometheuzz »

novice4eva wrote::D THANKS THANKS THANKS a trillion times :bow:
No problem.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: [SOLVED]Regex to check "at least one" containment

Post by GeertDD »

I think this modification is going to make it slightly faster:

Code: Select all

preg_match('/^(?=\D*+\d)(?=.*[~!@#$%^&*()])/', $password)
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: [SOLVED]Regex to check "at least one" containment

Post by novice4eva »

Thanks, will definitely try it :D
jokiruthi
Forum Newbie
Posts: 1
Joined: Thu Jan 29, 2009 3:52 am

Re: [SOLVED]Regex to check "at least one" containment

Post by jokiruthi »

Hi can u please tell me a reg expr pattern to check if the given string meets all the following criteria :

atleast 1 Uppercase
atleast 1 Lowercase
atleast 1 digit
atleast 1 SplCharacter
minimum 8 characters
only alphabets at start and end

Plz Help
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: [SOLVED]Regex to check "at least one" containment

Post by prometheuzz »

jokiruthi wrote:Hi can u please tell me a reg expr pattern to check if the given string meets all the following criteria :

atleast 1 Uppercase
atleast 1 Lowercase
atleast 1 digit
atleast 1 SplCharacter
minimum 8 characters
only alphabets at start and end

Plz Help
Please create a thread of your own instead of hijacking someone else's.
Your chances of getting a helpful reply will increase dramatically if you post what you have tried yourself. Your question in it's current form is just a plea to get someone else to do your work. Maybe someone will post a copy-and-paste solution for you, but I (and probably others too) tend to help those who have tried themselves first.

It also helps if you spell words out fully: the abundance of SMS abbreviated words is making my toes curl.

u = you
plz = please
SplCharacter = Special Character?
expr = expression

I mn, u dnt lk t whn ppl rspnd t ur qstns lk th, rght?

Okay, that's it, good luck.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: [SOLVED]Regex to check "at least one" containment

Post by papa »

prometheuzz wrote:
jokiruthi wrote:Hi can u please tell me a reg expr pattern to check if the given string meets all the following criteria :

atleast 1 Uppercase
atleast 1 Lowercase
atleast 1 digit
atleast 1 SplCharacter
minimum 8 characters
only alphabets at start and end

Plz Help
Please create a thread of your own instead of hijacking someone else's.
Your chances of getting a helpful reply will increase dramatically if you post what you have tried yourself. Your question in it's current form is just a plea to get someone else to do your work. Maybe someone will post a copy-and-paste solution for you, but I (and probably others too) tend to help those who have tried themselves first.

It also helps if you spell words out fully: the abundance of SMS abbreviated words is making my toes curl.

u = you
plz = please
SplCharacter = Special Character?
expr = expression

I mn, u dnt lk t whn ppl rspnd t ur qstns lk th, rght?

Okay, that's it, good luck.
L o l ! :wink:
Post Reply