Add Javascript to :confirm in form submit

I want to have some logic in my :confirm message, so that if the user enters some illegal data, it will notify directly when clicking on submit, and doesn't have to go through the server.

How can I add my Javascript code to the Rails :confirm attribute?

Can I do that with AJAX forms?

Thanks, Helzer