Form field validation
The controller you're posting to is responsible for the validation. In the case of the entries/saveEntry controller for example there's an Entry model populated and then run through the validation. Each model attribute and all the fields (content) have to give their ok for the complete model to validate correctly.

For the fields it's the field type's attribute type and an optional validate method that are responsible. Have a look at this plugin by Marion Newlevant, it provides a simple field type with configurable validation and is probably a good example for what you're trying to achieve.

https://github.com/marionnewlevant/craft-mn_matchinput