Page 1 of 1
[SOLVED]Regex to check "at least one" containment
Posted: Mon Jan 05, 2009 4:05 am
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
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!!

Re: Regex to check "at least one" containment
Posted: Mon Jan 05, 2009 4:53 am
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!)
Re: Regex to check "at least one" containment
Posted: Mon Jan 05, 2009 5:10 am
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

...
again thanks A LOT

Re: Regex to check "at least one" containment
Posted: Mon Jan 05, 2009 6:45 am
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

...
again thanks A LOT

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
Re: Regex to check "at least one" containment
Posted: Mon Jan 05, 2009 8:36 am
by novice4eva

THANKS THANKS THANKS a trillion times

Re: Regex to check "at least one" containment
Posted: Wed Jan 07, 2009 3:41 am
by prometheuzz
novice4eva wrote:
THANKS THANKS THANKS a trillion times

No problem.
Re: [SOLVED]Regex to check "at least one" containment
Posted: Sat Jan 10, 2009 7:12 am
by GeertDD
I think this modification is going to make it slightly faster:
Code: Select all
preg_match('/^(?=\D*+\d)(?=.*[~!@#$%^&*()])/', $password)
Re: [SOLVED]Regex to check "at least one" containment
Posted: Sun Jan 11, 2009 4:16 am
by novice4eva
Thanks, will definitely try it

Re: [SOLVED]Regex to check "at least one" containment
Posted: Thu Jan 29, 2009 3:56 am
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
Re: [SOLVED]Regex to check "at least one" containment
Posted: Thu Jan 29, 2009 4:01 am
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.
Re: [SOLVED]Regex to check "at least one" containment
Posted: Thu Jan 29, 2009 4:26 am
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 !
