The actual validation logic should really take place outside of your widget. You can configure your widget to handle and display validation feedback provided to it, just like the built-in widgets do. The widget boilerplate example should handle displaying feedback, and I believe the test project has a page to test this out.
As for setting up your validation logic, see here: https://world.mendix.com/display/public/howto50/Setting+up+data+validation
Mendix in general inherits from the Dijit/Dojo framework. You could try setting up your widget using the Dijit documentation for Form and ValidationTextBox