JQuery中一個簡單的表單驗證的執行個體

來源:互聯網
上載者:User

標籤:jquery   表單   

html代碼如下:

<!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 href="css/style.css" rel="stylesheet" type="text/css" />
  <!-- 引入jQuery -->
  <script src="../scripts/jquery-1.3.1.js" type="text/javascript"></script>
  <script type="text/javascript">
     //<![CDATA[
     $(function(){
        //如果是必填的,則加紅星標識
        $("form :input.required").each(function(){
           var $required = $("<strong class=‘high‘> *</strong>"); //建立元素
           $(this).parent().append($required); //然後將它追加到文檔中
        });
        //文字框失去焦點後
        $(‘form :input‘).blur(function(){
           var $parent = $(this).parent();
           $parent.find(".formtips").remove();
           //驗證使用者名稱
           if($(this).is(‘#username‘)){
             if(this.value=="" || this.value.length < 6){
                var errorMsg = ‘請輸入至少6位的使用者名稱.‘;
                $parent.append(‘<span class="formtips onError">‘+errorMsg+‘</span>‘);
             }else{
                var okMsg = ‘輸入正確.‘;
                $parent.append(‘<span class="formtips onSuccess">‘+okMsg+‘</span>‘);
             }
           }
           //驗證郵件
           if($(this).is(‘#email‘)){
              if(this.value=="" || ( this.value!="" && !/.+@.+\.[a-zA-Z]{2,4}$/.test(this.value)){
                  var errorMsg = ‘請輸入正確的E-Mail地址.‘;
                  $parent.append(‘<span class="formtips onError">‘+errorMsg+‘</span>‘);
              }else{
                  var okMsg = ‘輸入正確.‘;
                  $parent.append(‘<span class="formtips onSuccess">‘+okMsg+‘</span>‘);
              }
            }
      }).keyup(function(){
          $(this).triggerHandler("blur");
      }).focus(function(){
          $(this).triggerHandler("blur");
   });//end blur

  
  //提交,最終驗證
  $(‘#send‘).click(function(){
    $("form :input.required").trigger(‘blur‘);
    var numError = $(‘form .onError‘).length;
    if(numError){
       return false;
    }
    alert("註冊成功,密碼已發到你的郵箱,請查收.");
   });

  //重設
  $(‘#res‘).click(function(){
     $(".formtips").remove(); 
  });
})
//]]>
</script>
</head>
<body>

<form method="post" action="">
 <div class="int">
  <label for="username">使用者名稱:</label>
  <input type="text" id="username" class="required" />
 </div>
 <div class="int">
  <label for="email">郵箱:</label>
  <input type="text" id="email" class="required" />
 </div>
 <div class="int">
  <label for="personinfo">設定檔:</label>
  <input type="text" id="personinfo" />
 </div>
 <div class="sub">
  <input type="submit" value="提交" id="send"/><input type="reset" id="res"/>
 </div>
</form>

</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.