Validation ideas

I thought I was being clever by using a standard PHP function to check and see if all the characters in a field were alphanumeric. I never even thought about the fact that I need to allow spaces!!!

Is there a way to make exceptions? Say in one field I wanted alphanumeric, but spaces were allowed…and in another field, I allowed alphanumeric and spaces and commas, periods, exclamations…

Most of what I’m finding online is related to JavaScript, but JS can be turned off…not good for security!
I need to validate on the server side. Thanks.

You can use regular expressions to validate your fields. For example, if you want to allow alphanumeric with spaces, you can do this:
[php]if(!preg_match("~^[0-9a-z\x20]+$~i",$field1)){
echo ‘Invalid field1’;
}
[/php]

For alphanumeric and spaces and commas, periods, exclamations try:
[php]if(!preg_match("~^[0-9a-z\x20.,!]+$~i",$field2)){
echo ‘Invalid field2’;
}[/php]

More complex example, if you want to validate field to always start with 1 letter, followed with 4 to 10 alphanumeric or dashes:
[php]if(!preg_match("~^[a-z][0-9a-z-]{4,10}$~i",$field3)){
echo ‘Invalid field3’;
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service