- <!DOCTYPE html>
- <html>
- <head>
- <title>确认表单必填项完整性</title>
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="this is my page">
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <script type="text/javascript">
- function $(str){
- return (document.getElementById(str));
- }
- function check_submit(){
- if($("username").value ==""){
- alert("请填写用户名!");
- return false;
- }
- if($("password").value == ""){
- alert("请填写密码!");
- return false;
- }
- }
- </script>
- </head>
- <body>
- <form action="" onsubmit="return check_submit();">
- 用户名:<input id="username" type="text"><br/>
- 密 码:<input id="password" type="password"><br/>
- <input type="submit" value="提交">
- </form>
- </body>
- </html>
- 改进:
- <!DOCTYPE html>
- <html>
- <head>
- <title>更加人性化表单</title>
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="this is my page">
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <script type="text/javascript">
- function $(str){
- return (document.getElementById(str));
- }
- function check_submit(){
- if($("username").value =="" || $("username").value=="请输入用户名"){
- alert("用户名不能为空!");
- return false;
- }
- if($("password").value ==""){
- alert("密码不能为空!");
- return false;
- }
- }
- function mover(){
- event.srcElement.focus();
- event.srcElement.select();
- }
- function mclick(){
- if(event.srcElement.value=="请输入用户名")
- event.srcElement.value="";
- }
- function mblur(){
- if(event.srcElement.value=="")
- event.srcElement.value="请输入用户名";
- }
- </script>
- </head>
- <body>
- <form action="" onsubmit="return check_submit();">
- 用户名:<input id="username" type="text" onmouseover="mover();" onclick="mclick();"
- onblur="mblur();" value="请输入用户名"><br/>
- 密 码:<input id="password" type="password" onmouseover="mover();"><br/>
- <input type="submit" value="提交" id="submit">
- </form>
- </body>
- </html>
Friday, 20 April 2012
表单验证代码
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment