First, make sure that you do all your server-side validation. I like when my forms work without any JavaScript. I guess you have done so much.
**** ORIGINAL RESPONSE ***
Then change the "Submit" element to the button element. In the OnClick button element, run a JavaScript function that validates. Lots of samples on how to do this, as you know.
If verification fails, send alerts. If successful, use JavaScript to submit the form.
**** NEW, TOOL USING ANSWER ***
You can also use jQuery as (orip points), and these are plugins to do this. They do the hard work. Please make sure my comments tell the correct story. this code also provides AJAX.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.css" type="text/css" media="screen" /> <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script> <script type="text/javascript" src="http://jqueryjs.googlecode.com/svn/trunk/plugins/form/jquery.form.js"></script> <script type="text/javascript" language="javascript"> </script> <style type="text/css"> * { font-family: Verdana; font-size: 96%; } label { width: 10em; float: left; } label.error { float: none; color: red; padding-left: .5em; vertical-align: top; } p { clear: both; } .submit { margin-left: 12em; } em { font-weight: bold; padding-right: 1em; vertical-align: top; } </style> </head> <body> <form action="" id="newsletterform" method="get"> <input type="text" name="email" class="required email" id="textnewsletter" /> <input type="submit" id="signup" /> </form> </body> </html>
This is not the hardest code you could write, but it will serve as an example.
source share