CodeIgniter Forums
Public Comment field - form validation regex help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Public Comment field - form validation regex help (/showthread.php?tid=44719)



Public Comment field - form validation regex help - El Forum - 08-24-2011

[eluser]CroNiX[/eluser]
Regex is admittedly one of my lacking areas. I am trying to create a validation rule for a "comment" field on a public form that only accepts:
1) Alphanumeric characters
2) Basic punctuation/symbols which only include .,?!#$%*()-+=;: and single and double quotes
3) Carriage Returns

I believe it is safer to not allow <> and &

Here is what I have put together from some examples on the net and my own. I've been testing it and it seems to work, but if there is anything I am missing or improvements I would sure appreciate any pointers.
Code:
return (preg_match("/[^\w\s\;\.\-\,\%\$\!\?\'\"\:\*\(\)\+\=\#]/iD", $str)) ? FALSE : TRUE;

Thank you!

Edit: the code highlighter doesn't seem to like it and Zend Studio complains about it, but it still seems to work.