I was gonna suggest using a regular expression (the ereg() function), but then I was afraid you might ask me how to write one, so I decided not to mention it.
You might try a google search (for "regular expression", regex, grep, ereg, stuff like that). I found some help that way when trying to write similar validation routines. If you get lucky, you might even find a site that explains it in a way you can understand. There are a few out there.