Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex - whitespaces? 1

Status
Not open for further replies.

ScaryEders

Programmer
Dec 26, 2004
10
0
0
GB
i've been using:

Code:
ereg('^[a-zA-Z0-9]{3,23}$', $_POST['password'])

to check that a password only contains letters and numbers, but how do i make whitespaces OK as well?

i saw \s is meant to in a regex meant to mean a whitspace, so i tried [a-zA-Z0-9\s] but it thinks its a backslash and an s.

any help much appreciated,

ed.
 
try a literal space, otherwise tabs could be allowed:
Code:
 ereg('^[a-zA-Z0-9[highlight] [/highlight]]{3,23}$', $_POST['password'])

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
this is because u use the metacharacter \s in a character-class, where it has not the same meaning...

__________________________________________
a programmer is a tool that converts coffee into code...
aschi666, sthcomputing.de
 
what about trying something like [a-zA-Z0-9[:space:]] or say [[:alnum:][:space:]]. alnum represents numbers and alphabets. there is this other one : [:print:] .it allows alnums, spaces but also allows punctuation which i doubt u want.
anyway, i hope d above helps
 
thanks for ur help!

specially jemminger - why didn't i think of literal spaces?

sometimes with me, the obvious is the least obvious . . .

ed.

 
Final note:
I always prefer PCRE to the POSIX kind of regex. The PHP manual states
PHP manual said:
Tip: PHP also supports regular expressions using a Perl-compatible syntax using the PCRE functions. Those functions support non-greedy matching, assertions, conditional subpatterns, and a number of other features not supported by the POSIX-extended regular expression syntax.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top