11
Mithrandir
Re: Need help from Regular Expression Guru

Since you seem to have a real nack for this, Dave, could you help me with this?

I need a function to chek if a string contains upper-case letters, lower-case letters, numbers and spaces.

That possible/ease to do?

12
Dave_L
Re: Need help from Regular Expression Guru
  • 2004/1/14 1:39

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


I think this does what you want:

if (!preg_match('/[^A-Za-z0-9 ]/'$text)) {
   echo 
"Okn";
} else {
   echo 
"Not okn";
}



13
Mithrandir
Re: Need help from Regular Expression Guru

Could you explain the syntax? I find the preg_match function very confusing? (Not that I don't trust you just want to understand it )

14
Dave_L
Re: Need help from Regular Expression Guru
  • 2004/1/14 1:50

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


[A-Za-z0-9 ] is the character class containing the uppercase letters, lowercase letters, digits and space character.

[^A-Za-z0-9 ] is the character class of all characters not in the previous class.

So !preg_match('/[^A-Za-z0-9 ]/',$text) evaluates as true if $text does not contain a character that is not an uppercase letter, lowercase letter, digit or space.

The double negative makes it confusing.

15
Mithrandir
Re: Need help from Regular Expression Guru

It sure does - and I see that you actually read between the lines and made a function, which returns true only if ALL characters are either upper-case, lower-case, numbers or spaces. Well done and thanks

16
Mithrandir
Re: Need help from Regular Expression Guru



It ain't working.
I give it a text solely with lower-case numbers and it turns out true.

Which may be because I didn't specify the task well enough. It's meant for a password checker to ensure that a password contains BOTH lower-case, upper-case, numbers AND spaces.

17
Dave_L
Re: Need help from Regular Expression Guru
  • 2004/1/29 14:23

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


To rephrase:

Given a string, determine whether the string contains at least one uppercase letter (A-Z), at least one lowercase letter (a-z), at least one digit (0-9) and at least one space (ASCII 0x20).

If that's correct, do you really want spaces in passwords? I would think it would be better to have special characters, such as $ and %.

I'll see what I can come up with later, unless you or someone else solves it first. I'm not sure that a regex is the best way of handling this.

18
Mithrandir
Re: Need help from Regular Expression Guru

Spaces was requested by my customer. Is there anything "bad" about it?

I appreciate your help a lot. Thank you

19
Dave_L
Re: Need help from Regular Expression Guru
  • 2004/1/29 16:07

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


I suppose spaces are ok. It might make it a little easier for users to mistype their passwords, but maybe that's the intent.

This should work:

if (preg_match('/[A-Z]/'$text) and preg_match('/[a-z]/'$text) and preg_match('/[0-9]/'$text) and preg_match('/ /'$text)) {
   echo 
"Passedn";
} else {
   echo 
"Failedn";
}

20
Mithrandir
Re: Need help from Regular Expression Guru

Yep, that seemed to do it. Thanks a bunch.

Login

Who's Online

125 user(s) are online (82 user(s) are browsing Support Forums)


Members: 0


Guests: 125


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Apr 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits