I can't help you with the javascript, but I wanted to ensure you weren't relying on JavaScript as your only source of form validation. It is extremely insecure to perform any sort of validation on the client side, and if this is just a "helper function" to assist users that's cool, but if this is your own validation, then you should probably rethink your design.