1 <html>
 2   <head>
 3     <title></title>
 4        <script language="JavaScript">
 5       function show(){            //定义函数
 6      var value=myform.email.value;    //取得输入内容
 7    if(!/^\w+@\w+.\w+$/.test(value))  //对输入内容进行验证
 8    {
 9     alert("格式不正确")
10     myform.email.focus();      //焦点定位到email框
11    myform.email.select();    //选择全部内容
12     return false;    //错误 不提交
13  }
14  return true;
15  }
16          </script>
17        </head>
18      <body>
19  <form action="" method="post" name="myform" onSubmit="return show(this)"> //定义表单this指当前元素,即此表单
20    请输入内容:<input type="text" name="email">  //定义文本框
21  <input type="submit" value="提交">  //显示内容
22  </form>
23  </body>
24  </html>

本程序用onSubmit事件来验证提交表单内容,由于此事件决定表单是否被提交。所以用return来接受 show(this)的返回值,this表示当前元素,此事件在<form>元素调用。故this表示当前的<form>表单。email元素通过focus获得焦点,并通过select选则全部内容。