Stupid question.
I’ve got a project that I’m working on in PHP; it’s a registration form for a customer that will eventually write to a MySQL database. I’ve got the form doing data validation for the fields involved, and I’m already using some of PHP’s builtin filter functions.
However, I’ve got quite a few fields that I need to validate for different purposes – for example, I’ve got email addresses and web addresses to verify (which are covered in the filter package), but I’ve also got to verify phone numbers, zip codes, addresses, and names (which should only match a certain character set).
Right now, my solution consists of a lot of if-then statements to verify a field. For instance, for zipcode, I verify whether it’s numeric with ctype_digit, and then I use strlen to check the length. (I could encapsulate everything inside an object – and I will probably do so once I’ve got everything working as it actually should – but that’s a little ways away.) Does anyone know of a third-party class that will do stuff like this? Or, if not, at least a way to validate specific input types without resorting to if statements based on the input name. since it’s really not scalable?
If I ned to edit this question or add anything, let me know. Thank you.
Check out
FILTER_VALIDATE_REGEXP. It lets you do your own custom validations.If you hate regex, you can also use
FILTER_CALLBACK.