Forum Moderators: coopster

Message Too Old, No Replies

Matching quotes with preg_match

Need to allow single and double quotes in form input

         

MatthewHSE

8:37 pm on Feb 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've been using the following line to validate some form data. It's meant to reject the data if it contains anything BUT letters, numnbers, spaces, periods, and & symbols:

if (preg_match("/[^0-9a-z \.\&]/i", $Data)) {

I now find that I need to allow single and double quotes, too. I tried adding them with and without escaping them, but couldn't seem to make it work. I'm sure I'm missing something simple, but unfortunately I can't find the problem.

Ideas, anyone?

Thanks,

Matthew

coopster

9:28 pm on Feb 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I don't see single quotation or double quotation marks in your regular expression ...? Add them, save the script and test.

MatthewHSE

10:12 pm on Feb 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did add them, several different ways, and tested each. I pasted the code above without them because I wanted to know how to successfully add them. Here are a few of the variations I've already tried:

if (preg_match("/[^0-9a-z \.\&\'\"]/i", $Data)) {
if (preg_match("/[^0-9a-z \.\&'\"]/i", $Data)) {
if (preg_match("/[^0-9a-z \.\&'"]/i", $Data)) {
if (preg_match("/[^0-9a-z \.\&\'"]/i", $Data)) {

None of them work. It's got to be something simple but I'm overlooking it. (It's been a tough day; I'm trying to write something to replace a third-party script that was hacked this morning...)

vevs

10:21 pm on Feb 8, 2006 (gmt 0)

10+ Year Member



this one should be ok

if (preg_match("/^[a-zA-Z0-9.\&\'\" ]+$/",$Data)) {
echo 'ok';
};