使用Regex驗證登入頁面的輸入內容

來源:互聯網
上載者:User
這篇文章主要介紹了使用Regex驗證登入頁面輸入是否符合要求的執行個體代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

先給大家展示下:

廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:


<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> </head> <script src="js/jquery-1.8.0.min.js"></script> <script> $(function() {  $("input[name='uname']").blur(function() { //失去焦點  var namestr = $(this).val();  var regstr = /^[\u4e00-\u9fa5]{2,4}$/;  if(!regstr.test(namestr)) {   $(this).parent().next().html("使用者名稱必須是2-4個漢字").css("color", "red");   return false;  }  return true;  });   $("input[name = 'uname']").focus(function() { //擷取焦點  $(this).val("");  $(this).parent().next().html("");  });  $("input[name='pwd']").blur(function() {  var pwdstr = $(this).val();  var regstr = /^\w{6}$/;  if(!regstr.test(pwdstr)) {   $(this).parent().next().html("密碼必須是6位元字字母底線").css("color", "red");   return false;  }  return true;  });  $("input[name='pwd']").focus(function() {  $(this).parent().next().html("");  });  $("input[name='birthday']").blur(function() {  var birthdaystr = $(this).val();  var regstr = /^(19|20)\d{2}-(1[0-2]|0?[1-9])-(3[0-1]|2[0-9]|0?[1-9])$/;  if(!regstr.test(birthdaystr)) {   $(this).parent().next().html("日期格式不正確").css("color", "red");   return false;  }  return true;  });  $("input[name='birthday']").focus(function() {  $(this).parent().next().html("");  });  $("input[name='email']").blur(function(){  var emailstr = $(this).val();  var regstr = /^[\w\-]+@[a-z0-9A-Z]+(\.[a-zA-Z]{2,3}){1,2}$/;  if(!regstr.test(emailstr)){   $(this).parent().next().html("郵箱格式不正確").css("color","red");   return false;  }  return true;  });  $("input[name='email']").focus(function(){  $(this).parent().next().html("");  }); }); </script> <style> body {  font-size: 12px; } #home {  background-color: beige;  border: solid 1px black;  width: 550px;  height: 185px;  margin: auto;  margin-top: 20px; } #head {  height: 135px; } #foot {  text-align: center; } .dl1 {  clear: both;  padding-left: 10px; } .dl1 dt {  float: left;  height: 30px;  width: 80px;  line-height: 30px; } .dl1 dd {  float: left;  height: 30px;  line-height: 30px;  /*width: 250px;*/ } #btn_res {  background-image: url(img/reset.gif);  width: 80px;  height: 34px; } #btn_sub {  background-image: url(img/submit.gif);  width: 80px;  height: 34px; } </style> <body> <p id="home">  <p id="head">  <form action="" method="post">   <dl class="dl1">   <dt>使用者名稱 : </dt>   <dd class="dd1"><input type="text" value="輸入使用者名稱" name="uname" /></dd>   <dd></dd>   </dl>   <dl class="dl1">   <dt>使用者密碼 : </dt>   <dd class="dd1"><input type="password" name="pwd" /></dd>   <dd></dd>   </dl>   <dl class="dl1">   <dt>出生日期 : </dt>   <dd class="dd1"><input type="text" name="birthday" /></dd>   <dd>yyyy-mm-dd</dd>   </dl>   <dl class="dl1">   <dt>使用者郵箱 : </dt>   <dd><input type="text" name="email"/></dd>   <dd></dd>   </dl>  </form>  </p>  <p id="foot">  <input type="submit" value="" id="btn_sub" />  <input type="reset" value="" id="btn_res" />  </p> </p> </body></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.