Jump to content

Validating Form Data


zahidprimex
 Share

Recommended Posts

You could elaborate on the kind of data you need to validate, and what validation actually means to you.But mostly it comes down to matching and filtering strings with regular expressions passed into functions like preg_match(). So a good start might be to Google things like "preg_match email", "preg_match username." Look up tutorials on regular expressions.Also, starting off right will help a lot. For a lot of data, there is no reason why it should contain more characters than alphanumeric and underscore. (Text and passwords are a big exception.) If you start with that in mind, you can simplify your life considerably. Most users see such a restriction all the time, so it wouldn't throw them. For instance, more and more I see sites forbidding dashes, parentheses, and spaces in social security numbers, telephone numbers, credit card numbers, etc. It's just good practice.Watch out for backtick (`) characters!That's a start, anyway.

Link to comment
Share on other sites

In the case of things like phone or credit card numbers, I think that some things could use a little more intelligence. A phone number is only digits, so, for example, if you're looking for a US 10-digit number then you would check if the string is numeric and is 10 characters. I see some things come back with an error if you put the dashes or whatever in, so instead of showing an error in that case it might be better to just remove everything like that before you test it. Then they can put the number in whatever format they want and, as long as it's 10 digits, it will pass.

$strip = array(  '-',   '.',   '(',   ')',   ' ');$phone = str_replace($strip, '', $_POST['phone']);if (!is_numeric($phone) || strlen($phone) != 10){  echo "invalid phone";}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...