web登录页面jquery校验的简单实现(一)

      我们平时无论是做web开发还是移动端的app开发,都离不开登录页面。为了实现更好的用户体验,就需要添加用户名和用户密码的校验,甚至像记住密码等一些其它的功能。


**********************方式一:keyup监听键盘摁下事件

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/jquery.min.js"></script>
<title>Insert title here</title>
<style type="text/css">
   .fm{margin:30px 30px}
   .fz{font-size:12px;color:red;padding-left:10px}
</style>
</head>
<body>
   <form action="" method="" class="fm">
       <p><input type="text" id="username" placeholder="请输入账号"/><span class="fz nouser">* 用户名不能为空</span></p>
       <p><input type="password" id="password" placeholder="请输入密码"/><span class="fz nopwd">* 密码不能为空</span></p>
       <input type="button" id="btn" value="登录"/>
   </form>
   <script type="text/javascript">
       $("#username").keyup(function(){
    	  if($("#username").val.length<1){
    		  $(".nouser").show(); 
    	  }else{
    		  $(".nouser").hide();
    	  }
       });
   
       $("#password").keyup(function(){
    	   if($("#password").val.length<1){
    		   $(".nopwd").show();
    	   }else{
    		   $(".nopwd").hide(); 
    	   }
       })
   </script>
</body>
</html>


**********************方式二:propertychange监听对象属性值变化

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/jquery.min.js"></script>
<title>input2</title>
<style type="text/css">
   .fm{margin:30px 30px}
   .fz{font-size:12px;color:red;padding-left:10px}
</style>
</head>
<body>
   <form action="" method="" class="fm">
       <p><input type="text" id="username" placeholder="请输入账号"/><span class="fz nouser">* 用户名不能为空</span></p>
       <p><input type="password" id="password" placeholder="请输入密码"/><span class="fz nopwd">* 密码不能为空</span></p>
       <input type="button" id="btn" value="登录"/>
   </form>
   <script type="text/javascript">
       $("#username").bind("input propertychange",function(){
    	  if($("#username").val.length<1){
    		  $(".nouser").show(); 
    	  }else{
    		  $(".nouser").hide();
    	  }
       });
   
       $("#password").bind("input propertychange",function(){
    	   if($("#password").val.length<1){
    		   $(".nopwd").show();
    	   }else{
    		   $(".nopwd").hide(); 
    	   }
       })
   </script>
</body>
</html>


**********************方式三:与服务器后台交互的校验

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/jquery.min.js"></script>
<title>input3</title>
<style>
   .info{
      color:red;
   }
</style>
</head>
<body>
    <form action="">
       <label for="user">用户:</label>
       <input type="text" id="username" placeholder="用户名"/>
       //<label id="userInfo" class="info"></label><br><br>
       
       <label for="password">密码:</label>
       <input type="password" id="password" placeholder="密码"/>
       //<label id="passwordInfo" class="info"></label><br><br>
       <label id="infoLabel" class="info"></label>
       
       <input type="button" value="登录" οnclick="login()"/>
    </form>
    
    
    	<script>
			var ctx;
			
			$(function(){
				hideAllInfo();
				ctx = getRootPath();
				//alert(path);
			});
			
			function getRootPath(){
				var strFullPath=window.document.location.href;
				var strPath=window.document.location.pathname;
				var pos=strFullPath.indexOf(strPath);
				var prePath=strFullPath.substring(0,pos);
				var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);
				return(prePath+postPath);
			}
			
			//表单验证
			function checkForm(userName, password){
				//验证邮箱是否为空
				if(userName==null || userName=="undefined" || typeof(userName)=="undefined" || userName.length==0){
					hideInfo("infoLabel");
					showInfo("infoLabel", "用户名不能为空");
					return false;
				}
				//验证邮箱格式
				var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
				if(!reg.test(userName)){
					hideInfo("infoLabel");
					showInfo("infoLabel", "请输入正确邮箱格式");
					return false;
				}
				//验证密码是否为空
				if(password==null || password=="undefined" || typeof(password)=="undefined" || password.length==0){
					hideInfo("infoLabel");
					showInfo("infoLabel", "密码不能为空");
					return false;
				}
				
				return true;
			}
			//登录
			function login(){
				
				hideAllInfo();
				
				var userName = $("#username").val();
				var password = $("#password").val();
				if(checkForm(userName, password)){
					$.post(
						ctx+"************",
						*****************
						{"userName":userName, "password":password, "randMath":Math.random()*Math.random()},
						function(result){
							if(result.status == "0"){
								window.location.href = "**********************";
							}else{
								showInfo("infoLabel", result.msg);
							}
						},
						"json"
					);
				}
			}
			
			//显示提示信息
			function showInfo(id, html){
				$("#"+id).html("*"+html);
				$("#"+id).css("display","block");
			}
			
			//隐藏提示信息
			function hideInfo(id){
				$("#"+id).html("");
				$("#"+id).css("display","none");
			}
			
			//隐藏所有提示信息
			function hideAllInfo(){
				hideInfo("infoLabel");
				hideInfo("emailInfoLabel");
				hideInfo("passwordInfoLabel");
			}
			
		</script>
</body>
</html>


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会为你提供相关的技术要求和代码实现。 技术要求: 1.前端使用div进行布局,页面要有基本设计元素; 2.使用AJAX+JSON形式,实现前后交互。 代码实现: HTML代码: ``` <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>学生信息管理系统</title> </head> <body> <header> <h1>学生信息管理系统</h1> <nav> <ul> <li><a href="#">首页</a></li> <li><a href="#">新增学生</a></li> <li><a href="#">修改学生</a></li> <li><a href="#">删除学生</a></li> <li><a href="#">条件查询</a></li> <li><a href="#">全部查询</a></li> </ul> </nav> </header> <main> <form name="loginForm"> <fieldset> <legend>登录</legend> <label for="username">用户名:</label> <input type="text" name="username" id="username" required> <label for="password">密码:</label> <input type="password" name="password" id="password" required> <input type="submit" value="登录"> </fieldset> </form> </main> <footer> <p>版权所有:XXX公司</p> </footer> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script src="js/main.js"></script> </body> </html> ``` CSS代码: ``` /* 全局样式 */ body { font-family: "微软雅黑", Arial, sans-serif; margin: 0; padding: 0; } header { background-color: #333; color: #fff; padding: 10px; } nav ul { list-style: none; margin: 0; padding: 0; } nav ul li { display: inline-block; } nav ul li a { color: #fff; text-decoration: none; padding: 10px; } main { padding: 10px; } footer { background-color: #333; color: #fff; padding: 5px; text-align: center; } /* 登录页面样式 */ form { border: 1px solid #ccc; border-radius: 5px; padding: 10px; width: 300px; margin: 0 auto; } legend { font-weight: bold; font-size: 20px; } label { display: block; margin-top: 10px; } input[type=text], input[type=password] { border: 1px solid #ccc; border-radius: 5px; padding: 5px; width: 100%; } input[type=submit] { background-color: #333; color: #fff; padding: 5px 10px; border: none; border-radius: 5px; cursor: pointer; margin-top: 10px; } input[type=submit]:hover { background-color: #444; } ``` JavaScript代码: ``` $(function() { //登录表单提交 $('form[name=loginForm]').submit(function(event) { event.preventDefault(); //获取用户名和密码 var username = $('#username').val(); var password = $('#password').val(); //发送AJAX请求 $.ajax({ url: 'login.php', type: 'post', dataType: 'json', data: {username: username, password: password}, success: function(data) { if (data.code == 200) { alert('登录成功!'); window.location.href = 'index.php'; } else { alert('登录失败!' + data.message); } }, error: function() { alert('登录失败!'); } }); }); }); ``` PHP代码(login.php): ``` <?php //模拟登录 $username = $_POST['username']; $password = $_POST['password']; if ($username == 'admin' && $password == 'admin') { $result = array('code' => 200, 'message' => '登录成功!'); } else { $result = array('code' => 500, 'message' => '用户名或密码错误!'); } echo json_encode($result); ?> ``` 以上是一个简单的学生信息管理系统的代码实现,具有登录页面和各种功能页面,使用AJAX+JSON形式实现前后交互,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值