This is yet another form validation question. I'm in the process of going back over how I handle form validation. There doesn't seem to an overwhelming consensus on how best to handle the simpler fields. I can find tons on how to validate email addresses.
I've always used preg_match to check for allowable characters, lengths and patterns. One of my forms is simply to gather basic user info, name, address, city, etc. When it comes to things like zip codes and phone numbers, it's pretty straight forward. Only allow numbers for zips. And, only numbers, spaces and dashes for phone numbers (and maybe parentheses).
Should I be using preg_match for things like first name, last name, street address? These fields seem to be the ones that will be impossible to plan for every weird spelling or formatting. Is it better to just check for string length, existence (if required) and send it to the database via mysql_real_escape_string? I've always been hesitant and have followed it's better safe than sorry when handling user input. But, I might have it all wrong? Any ideas, or help?
Names
preg_match("/^[A-Za-z0-9-_\s]{3,30}$/", $string)
Address
preg_match("/^[A-Za-z0-9-_.#\s]{0,80}$/", $string)
City
preg_match("/^[A-Za-z0-9\s]{4,32}$/", $string)