5

Would very much appreciate some help. My hosts server-side updates have caused my comment form to throw two validation errors. First one is: preg_match() returns Warning: preg_match() [function.preg-match]: Compilation failed: range out of order in character class at offset 13 in [script location here and line error] - second is on subject check at offset 14.

It does this on the email address check:

if (preg_match('/[^a-zA-Z0-9_-.]/', $_POST['txtEmail']))

and subject check:

if (preg_match('/[^a-zA-Z0-9:?-. ]/', $_POST['txtSubject']))

I'm not familiar with PHP but can cut and paste! My website's frowey.com and it's the comments form on contact us that's started throwing an error after hosting OS updates. Thanks in advance.

mario
  • 144,265
  • 20
  • 237
  • 291
mochj
  • 51
  • 1
  • 3

2 Answers2

9

You need to escape the - minus. It has special meaning with character classes, as the error message hints. Use a backslash before the minus:

preg_match('/[^a-zA-Z0-9_\-.]/'

(Alternatively the - may be the first or last thing in the character group, so it loses its special function.)

mario
  • 144,265
  • 20
  • 237
  • 291
1
if (preg_match('/[^a-zA-Z0-9_\.-]/', $_POST['txtEmail']))

if (preg_match('/[^a-zA-Z0-9:\?\.-]/', $_POST['txtSubject']))
  • Big thanks for both answers. I'll check it out now and let you know outcome. – mochj Oct 27 '11 at 15:29
  • Sorted! Thanks. Also needed to make a change to email address validation. All now works. Thanks again. – mochj Oct 27 '11 at 15:54