我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

验证表单的标准JavaScript

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    
<head>
        
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
        
<title>Untitled Document</title>
        
<script type="text/javascript">
            
function validate(m,n)
            
{
                
var flag = true;
                
if(m.value == "" && n == 0)
                
{
                    document.getElementById(
"username_info").innerHTML = "用户名不为能空";
                    flag 
= false;
                }

                
else
                
{
                    document.getElementById(
"username_info").innerHTML = "";
                }

                
if(m.value.length < 6 && n == 0)
                
{
                    document.getElementById(
"username_info").innerHTML = "用户名长度不能小于6";
                    flag 
= false;
                }

                
else
                
{
                    document.getElementById(
"username_info").innerHTML = "";
                }

                
if(m.value == "" && n == 1)
                
{
                    document.getElementById(
"password_info").innerHTML = "密码不能为空";
                    flag 
= false;
                }

                
else
                
{
                    document.getElementById(
"password_info").innerHTML = "";
                }

                
if(m.value.length < 6 && n == 1)
                
{
                    document.getElementById(
"password_info").innerHTML = "密码长度不能小于6";
                    flag 
= false;
                }

                
else
                
{
                    document.getElementById(
"password_info").innerHTML = "";
                }

                
return flag;
                
            }

        
</script>
    
</head>
    
<body>
        
<div align="center">
            
<form>
                
<input type="text" id="username" onblur="validate(this,0);"/><div id="username_info"></div>
                
<br/>
                
<input type="password" id="password" onblur="validate(this,1);"/><div id="password_info"></div>
                
<br/>
                
<input type="submit" value="验证" /><input type="reset" value="重置"/>
                
<br/>
            
</form>
        
</div>
    
</body>
</html>


posted on 2007-12-06 08:11 々上善若水々 阅读(557) 评论(0)  编辑  收藏 所属分类: JavaScript


只有注册用户登录后才能发表评论。


网站导航: