By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,746 Members | 1,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,746 IT Pros & Developers. It's quick & easy.

Regular Expression to validate password

P: n/a
Hi,

I need to validate password keyed in by the system users so that the
password will contain only letters and numbers plus at least one
capital letter. Exclude these symbols

, < ? / * ( ) & ^ % $ # ! ~ ` " '

this is my code:

ereg("[A-Za-z0-9[^\,\<\>\?\/\*\(\)\&\^\%\$\#\!\~\`\"\']]+$", $str)

am i correct?

May 7 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
..oO(adzir)
>I need to validate password keyed in by the system users so that the
password will contain only letters and numbers plus at least one
capital letter. Exclude these symbols

, < ? / * ( ) & ^ % $ # ! ~ ` " '

this is my code:

ereg("[A-Za-z0-9[^\,\<\>\?\/\*\(\)\&\^\%\$\#\!\~\`\"\']]+$", $str)

am i correct?
Way too complicated and using the old deprecated ereg* functions.

If the PW should only contain letters and digits, then use a pattern
that matches for exactly that:

preg_match('#[A-Z\d]+#i', $str);

If there has to be a capital letter somewhere in the PW, then try a
combination like this:

preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);

HTH
Micha
May 7 '07 #2

P: n/a
On May 7, 11:53 pm, Michael Fesser <neti...@gmx.dewrote
Way too complicated and using the old deprecated ereg* functions.

If the PW should only contain letters and digits, then use a pattern
that matches for exactly that:

preg_match('#[A-Z\d]+#i', $str);

If there has to be a capital letter somewhere in the PW, then try a
combination like this:

preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);

HTH
Micha
Hey thanks,

I was referring an old book. I didn't noticed that there is another
function best suits to my needs. My broadband was out for a week and I
was stuck dead.

but u didn't answer my another question which was to get rid of the
symbols? anyway thanks a lot

May 7 '07 #3

P: n/a
Daz
On May 7, 5:08 pm, adzir <muad...@yahoo.comwrote:
On May 7, 11:53 pm, Michael Fesser <neti...@gmx.dewrote
Way too complicated and using the old deprecated ereg* functions.
If the PW should only contain letters and digits, then use a pattern
that matches for exactly that:
preg_match('#[A-Z\d]+#i', $str);
If there has to be a capital letter somewhere in the PW, then try a
combination like this:
preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
HTH
Micha

Hey thanks,

I was referring an old book. I didn't noticed that there is another
function best suits to my needs. My broadband was out for a week and I
was stuck dead.

but u didn't answer my another question which was to get rid of the
symbols? anyway thanks a lot
I didn't notice that there was another question. In any case, check
out strtr(), or preg_replace().

May 7 '07 #4

P: n/a
..oO(adzir)
>but u didn't answer my another question which was to get rid of the
symbols? anyway thanks a lot
You don't have to. Just match on the allowed characters. If someone
enters a $, # or whatever, the matching will fail.

Micha
May 7 '07 #5

P: n/a
Michael Fesser wrote:
.oO(adzir)
>I need to validate password keyed in by the system users so that the
password will contain only letters and numbers plus at least one
capital letter. Exclude these symbols

, < ? / * ( ) & ^ % $ # ! ~ ` " '

this is my code:

ereg("[A-Za-z0-9[^\,\<\>\?\/\*\(\)\&\^\%\$\#\!\~\`\"\']]+$", $str)

am i correct?

Way too complicated and using the old deprecated ereg* functions.

If the PW should only contain letters and digits, then use a pattern
that matches for exactly that:

preg_match('#[A-Z\d]+#i', $str);

If there has to be a capital letter somewhere in the PW, then try a
combination like this:

preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);

HTH
Micha
Michael, your pattern will also match "*&(*%& Z #)&*(", because it's not
anchored. Use ^ and $ (and not forget /D) to make it do what you want.

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 7 '07 #6

P: n/a
..oO(gosha bine)
>Michael Fesser wrote:
>preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
Michael, your pattern will also match "*&(*%& Z #)&*(", because it's not
anchored. Use ^ and $ (and not forget /D) to make it do what you want.
Correct. Good point.

Micha
May 7 '07 #7

P: n/a
On May 8, 1:31 am, Michael Fesser <neti...@gmx.dewrote:
.oO(gosha bine)
Michael Fesser wrote:
preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
Michael, your pattern will also match "*&(*%& Z #)&*(", because it's not
anchored. Use ^ and $ (and not forget /D) to make it do what you want.

Correct. Good point.

Micha
Where can I find a good site to explain to me all the syntax (simbols,
characters used in the preg_match function)?

php.net seems not to satisfy me.

May 11 '07 #8

P: n/a
>I need to validate password keyed in by the system users so that the
>password will contain only letters and numbers plus at least one
capital letter.
Gee, many administrators would like to PROHIBIT such passwords on the
grounds that they are "too obvious".
May 11 '07 #9

P: n/a
adzir wrote:
On May 8, 1:31 am, Michael Fesser <neti...@gmx.dewrote:
>.oO(gosha bine)
>>Michael Fesser wrote:
preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
Michael, your pattern will also match "*&(*%& Z #)&*(", because it's not
anchored. Use ^ and $ (and not forget /D) to make it do what you want.
Correct. Good point.

Micha

Where can I find a good site to explain to me all the syntax (simbols,
characters used in the preg_match function)?

php.net seems not to satisfy me.
Just google for "Regular expressions". There are thousands of them out
there, offering all levels of explanations. Many are pretty good.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 11 '07 #10

P: n/a
Daz
On May 11, 4:05 am, adzir <muad...@yahoo.comwrote:
On May 8, 1:31 am, Michael Fesser <neti...@gmx.dewrote:
.oO(gosha bine)
>Michael Fesser wrote:
>preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
>Michael, your pattern will also match "*&(*%& Z #)&*(", because it's not
>anchored. Use ^ and $ (and not forget /D) to make it do what you want.
Correct. Good point.
Micha

Where can I find a good site to explain to me all the syntax (simbols,
characters used in the preg_match function)?

php.net seems not to satisfy me.
www.regular-expressions.info is a good place to start. :)

May 11 '07 #11

P: n/a
At Thu, 10 May 2007 20:05:32 -0700, adzir let his monkeys type:
On May 8, 1:31 am, Michael Fesser <neti...@gmx.dewrote:
>.oO(gosha bine)
>Michael Fesser wrote:
>preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
>Michael, your pattern will also match "*&(*%& Z #)&*(", because it's not
anchored. Use ^ and $ (and not forget /D) to make it do what you want.

Correct. Good point.

Micha

Where can I find a good site to explain to me all the syntax (simbols,
characters used in the preg_match function)?

php.net seems not to satisfy me.
Kudos! Now *that's* the question so many fail to ask. The detours people
will take just to avoid learning the ropes with regex, unbelievable.

It _has_ a fairly steep learning curve and a level of abstraction that
requires concentration and focus, but the merits are well worth the effort
imho.

One catch (from my own experience): try to keep an open mind as to
what solution is needed in a given situation. Some code is full of regex
fixing stuff that could (should?!) easily be dealt with using normal
string functions. Often easier to follow/adapt (for you and future editors
of your code, good documentation explaining what your regex does is key.)
and less resource-hungry. A complex regular expression can be quite taxing
on your server.

The 'best' excuse found here to avoid the matter in the NG undoubtedly
was: "I don't trust regex!".

There are several sites that offer, besides explanation, on- or offline
utilities allowing you to experiment with regex on the fly. Be aware of
the subtle dialect differences between different regex flavours though. It
may prevent you from going ape at some point...

Good luck learning regex. You'll soon reach the point where you start
wondering how you ever managed without.

Sh.
May 12 '07 #12

P: n/a
adzir wrote:
On May 8, 1:31 am, Michael Fesser <neti...@gmx.dewrote:
>.oO(gosha bine)
>>Michael Fesser wrote:
preg_match('#[a-zA-Z\d]*[A-Z][a-zA-Z\d]*#', $str);
Michael, your pattern will also match "*&(*%& Z #)&*(", because it's not
anchored. Use ^ and $ (and not forget /D) to make it do what you want.
Correct. Good point.

Where can I find a good site to explain to me all the syntax (simbols,
characters used in the preg_match function)?

php.net seems not to satisfy me.
http://weitz.de/regex-coach/

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
May 12 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.