I assume you mean that you want to prevent spammers from using a web form to send spam messages?
What you are looking for is called a
CAPTCHA[
^] test.
You can google for a library that is free to use and don't need to reinvent the wheel (http://www.captcha.net/).
Cheers.