Rumah >hujung hadapan web >tutorial js >js表单登陆验证示例

js表单登陆验证示例

高洛峰
高洛峰asal
2016-12-09 16:08:271069semak imbas

本文实例讲述了js表单登陆验证的方法。分享给大家供大家参考,具体如下:

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>项目后台管理系统</title>
<link rel="stylesheet" href="__PUBLIC__/css/login.css" />
<script type="text/javascript" src="__PUBLIC__/js/jquery.min.js"></script>
<script type="text/javascript">
function $(id)
{
  return document.getElementById(id);
}
//验证姓名
function checkname(){
  var name=$("admin_name").value;
  if(name==&#39;&#39;)
  {
   $(&#39;s1&#39;).innerHTML=&#39;姓名不能为空&#39;;
   $(&#39;s1&#39;).style.color=&#39;red&#39;;
   return false;
  }
  else
  {
    $(&#39;s1&#39;).innerText=&#39;ok&#39;;
    $(&#39;s1&#39;).style.color=&#39;green&#39;;
    return true;
  }
}
//验证密码
function checkpwd(){
  var password=$("password").value;
  if(password==&#39;&#39;)
  {
   $(&#39;s2&#39;).innerHTML=&#39;密码不能为空&#39;;
   $(&#39;s2&#39;).style.color=&#39;red&#39;;
   return false;
  }
  else
  {
    $(&#39;s2&#39;).innerHTML=&#39;ok&#39;;
    $(&#39;s2&#39;).style.color=&#39;green&#39;;
    return true;
  }
}
//验证所有表单提交
function checkall()
{
  if(checkname()&&checkpwd())
  {
    return true;
  }
  else
  {
    return false;
  }
}
</script>
</head>
<body class="b">
<div class="lg">
<form action="__URL__/dologin" method="POST" onsubmit="return checkall();">
  <div class="lg_top"></div>
  <div class="lg_main">
    <div class="lg_m_1">
    <input name="admin_name" value="" class="ur" id="admin_name" onblur="checkname();"/><span id=&#39;s1&#39;></span>
    <input name="password" type="password" value="" class="pw" id="password" onblur="checkpwd();"/><span id=&#39;s2&#39;></span>
    </div>
  </div>
  <div class="lg_foot">
  <input type="submit" value="Login In" class="bn" /></div>
</form>
</div>
</body>
</html>

   


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn