Forum Moderators: coopster

Message Too Old, No Replies

Validation and preg_match

Adding more acceptable characters

         

Sovi3t

1:05 am on Feb 23, 2005 (gmt 0)

10+ Year Member



Alright, I installed a shopmod for my new phpBB. This mod is pretty bad. It took forever to install and forever to get rid of the bugs. When I got it to stop giving me errors, I found out some of the features weren't even working or activated in the first place.

Well, I've spent many many hours on this and i'm finally getting it to work. Now i'm fixing up the little stuff and adding/ removing some features.I am setting up custom titles and i'd like people to be use basic characters such as exclamation marks, periods, and possible dashes.

This is the line that controls validation:

elseif ((!preg_match("/^[.!a-zA-Z0-9]*$/", $newtitle)) ¦¦ (strlen($newtitle) < 2)) { message_die(GENERAL_MESSAGE, 'That Rank is Invalid, it must only contain characters A-Z, a-z and 0-9. For more specific titles talk to an admin.'); }

I tried googling this but all the examples I found were mainly just for email addresses and showed you how to use the code, not write it. :evil:

How would I have to modify this code to get more characters?

thx

[edited by: coopster at 11:27 am (utc) on Feb. 23, 2005]
[edit reason] removed url per TOS [webmasterworld.com] [/edit]

Sovi3t

4:45 am on Feb 23, 2005 (gmt 0)

10+ Year Member



Sorry, I posted the code in the above post incorrectly. The actual code it uses is:
elseif ((!preg_match("/^[a-zA-Z0-9]*$/", $newtitle)) ¦¦ (strlen($newtitle) < 2)) { message_die(GENERAL_MESSAGE, 'That Rank is Invalid, it must only contain characters A-Z, a-z and 0-9. For more specific titles talk to an admin.'); }

ironik

12:01 am on Feb 24, 2005 (gmt 0)

10+ Year Member



/^[\w\W]+$/

That regex will allow users to post data that are words (alpha numerics) and non-words (!*&%#@-_ and so on). It also requires that there be at least 1 character in the match. If you need to further restrict it, say allow words, but only the period and question mark characters then:

/^[\w\.\?]+?$/

(I've escaped the . and? characters with a backslash as they have a special meaning in regular expressions)

I'm not 100% sure about permitting a! character as it is used in regex's, but only in combination (What I mean to say is that you can use it, but I don't know if it has to be escaped with a backslash).