jQuery 表單驗證

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   sp   div   on   

1.判斷文本是否為空白:

1  $(".commonVerify").blur(function(){2        if($.trim($(this).val())==‘‘){3            $("#tips").html("不可為空");4            $(this).focus();}5     });

$.trim()是去掉字串的前後括弧。

2.判斷電話號碼:

 1    //判斷電話號碼是否正確 2     $(".mobil").blur(function(){ 3         if(!($.trim($(this).val())==‘‘)){ 4             if(!$(this).val().match(/^1[3|4|5|8][0-9]\d{4,8}$/)){ 5                 $(this).focus(); 6                 $("#tips").html("電話號碼錯誤"); 7             }else{ 8                 $("#tips").html(""); 9             }10         }else{11             $(this).focus();12             $("#tips").html("不可為空");13         }14     });15    

3.判斷郵遞區號是否正確:

 1     //判斷郵遞區號是否正確 2    $("#postalCode").blur(function(){ 3        if(!($.trim($(this).val())==‘‘)){ 4            if(!$(this).val().match(/^[1-9]\d{5}$/)){ 5                $(this).focus(); 6                $("#tips").html("郵遞區號錯誤:應為6位元"); 7            }else{ 8                $("#tips").html(""); 9            }10        } else{11            $(this).focus();12            $("#tips").html("不可為空");13        }14    });

4.判斷身份證是否正確:

 1  $("#idCard").blur(function() { 2         var idCard = $("#idCard").val(); 3         var bo = /^(\d{6})(18|19|20)?(\d{2})([01]\d)([0123]\d)(\d{3})(\d|X)?$/.test(idCard); 4         var year = idCard.substr(6, 4); 5         var month = idCard.substr(10, 2); 6         var day = idCard.substr(12, 2); 7  8         if(!($.trim($(this).val())==‘‘)){ 9             if (bo == false || month > 12 || day > 31) {10 11                 $("#tips").html("請輸入正確的社會安全號碼碼!");12             }else{13                 $("#tips").html("");14             }15         }else{16             $(this).focus();17             $("#tips").html("不可為空");18         }19     });

5.判斷銀行卡號:

 1 $("#bankCard").blur(function(){ 2         if(!($.trim($(this).val())==‘‘)){ 3             if(!$(this).val().match(/^\d{19}$/)){ 4                 $(this).focus(); 5                 $("#tips").html("格式錯誤,應該為19位元字"); 6             }else{ 7                 $("#tips").html(""); 8             } 9         } else{10             $(this).focus();11             $("#tips").html("不可為空");12         }13 14     })

 

jQuery 表單驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.