最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS簡(jiǎn)單表單驗(yàn)證功能完整示例

 更新時(shí)間:2020年01月26日 12:48:32   作者:echo曦  
這篇文章主要介紹了JS簡(jiǎn)單表單驗(yàn)證功能,結(jié)合完整實(shí)例形式分析了JavaScript表單驗(yàn)證相關(guān)的字符串判斷、正則驗(yàn)證、計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了JS簡(jiǎn)單表單驗(yàn)證功能。分享給大家供大家參考,具體如下:

簡(jiǎn)單js表單驗(yàn)證demo

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <title>Document</title>
  <script>
    //當(dāng)用戶名獲取焦點(diǎn)時(shí)
    function focus_username(){
      document.getElementById("user_res").innerHTML="<font color='#f00'>請(qǐng)輸入用戶名</font>";
    }
    //當(dāng)用戶名失去焦點(diǎn)時(shí)
    function blur_username(){
      var user_value=document.getElementsByName("username")[0].value;
      if(user_value.length===0){
      document.getElementById("user_res").innerHTML="<font color='#f00'>你沒(méi)有輸入用戶名</font>";
          return false;
          //判斷其長(zhǎng)度是否在5~18之間 如果不在就提示用戶
      }else if(user_value.length<5||user_value.length>18)
      {
      document.getElementById("user_res").innerHTML="<font color='#f00'>用戶名長(zhǎng)度必須在5-18之間</font>";
          return false;
      }else if(!checkUser(user_value)){
        //用戶名還有特殊符號(hào)
      document.getElementById("user_res").innerHTML="<font color='#f00'>用戶名含有特殊符號(hào)</font>";
          return false;
      }else{
        //用戶名合法
      document.getElementById("user_res").innerHTML="<font color='#00f'>用戶名合法</font>";
          return true;
      }
    }
    //密碼獲取焦點(diǎn)時(shí)
    function focus_password(){
      document.getElementById("pass_res").innerHTML="<font color='#f00'>請(qǐng)輸入密碼</font>";
    }
    //密碼失去焦點(diǎn)時(shí)
    function blur_password(){
      var user_value=document.getElementsByName("password")[0].value;
      if(user_value.length===0){
      document.getElementById("pass_res").innerHTML="<font color='#f00'>你沒(méi)有輸入密碼</font>";
          return false;
          //判斷其長(zhǎng)度是否在5~18之間 如果不在就提示用戶
      }else if(user_value.length<5||user_value.length>18)
      {
      document.getElementById("pass_res").innerHTML="<font color='#f00'>用密碼長(zhǎng)度必須在5-18之間</font>";
          return false;
      }else{
        //密碼合法
      document.getElementById("pass_res").innerHTML="<font color='#00f'>密碼合法</font>";
          return true;
      }
    }
     function checkUser(user){
      var arr=["<",">","#","?","%"];
      var arr_length=arr.length;
      var user_length=user.length;
      for(var i=0;i<arr_length;i++){
        for(var j=0;j<user_length;j++){
          if(arr[i]===user.charAt(j)){
            return false;
          }
        }
      }
      //表示用戶名合法
       return true;
     }
     //提交提交表單驗(yàn)證
     function checkForm(){
      var user_flag=blur_username();
      var pass_flag=blur_password();
      if(user_flag && pass_flag){
        alert("提交合法表單");
         return true;
      }else{
        alert("輸入不合法");
        return false;
      }
     }
  </script>
</head>
<body>
                          <!--action 參數(shù)自定義跳轉(zhuǎn)頁(yè)面-->
  <form name='form1' onsubmit='return checkForm()' action='index.php'>
    <table width='600' align='center'>
      <tr>
        <td align='right' width='150'>用戶名:</td>
        <td width='100'><input type='text' name='username' onfocus='focus_username()' onblur = 'blur_username()'/></td>
        <td><span id="user_res"></span></td>
      </tr>
      <tr>
        <td align='right' width='100'>密碼:</td>
        <td width='100'><input type='password' name='password' onfocus='focus_password()' onblur = 'blur_password()'/></td>
        <td><span id="pass_res"></span></td>
      </tr>
      <tr>
        <td></td>
        <td><input type='submit' value='提交' /></td>
      </tr>
    </table>
  </form>
</body>
</html>

運(yùn)行結(jié)果:

PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:

JavaScript正則表達(dá)式在線測(cè)試工具:
http://tools.jb51.net/regex/javascript

正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript正則表達(dá)式技巧大全》、《JavaScript表單(form)操作技巧大全》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

青田县| 海安县| 肇州县| 西盟| 达州市| 大城县| 讷河市| 阿拉善左旗| 阳谷县| 卫辉市| 郓城县| 通江县| 汾西县| 潼南县| 宝山区| 沙河市| 祁门县| 海晏县| 宾阳县| 黄骅市| 榕江县| 昔阳县| 惠东县| 台北市| 五莲县| 长武县| 罗定市| 大名县| 庄河市| 文安县| 河间市| 阿克| 合肥市| 建平县| 财经| 北安市| 如东县| 吉木乃县| 洪洞县| 庆阳市| 连山|