<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <title>Form 校验</title> <script> function CheckForm(form) { if(form.username.value == "") { alert('名字总要填一个吧!'); form.username.focus(); return false } return true; } </script> </head> <body> <form name="form1" id="form1" action="" method="post" onsubmit="CheckForm(this)"> <input type="text" name="username"> <input type="submit" value=" 提交"> </form> </body> </html>