jquery非空验证功能

时间:2023-03-09 14:26:41
jquery非空验证功能

<script type="text/javascript">
        
        $(function(){
          /**************验证用户名********************************************/
          $("#Tbpetname").blur(function(){
          //验证宠物名长度 失去焦点事件
           var ck=true;
           var pname= $(this).val().length;
           if(pname<1)
           {
               alert("宠物名不能为空");
               ck=false;
           }
           else
           {
              if(pname>20){
              alert("宠物名称过长,换一个试试?");
              ck=false;
              }
           }
           
           if(!ck)
           {
            $(this).next(".spanlv").css("color","Red");
           }
           
          });

$("#Tbpetname").focus(function(){
          // 获得焦点事件
           $(this).next(".spanlv").css("color","#8ec63f");
          
          })

});

</script>

<div class="panel">
               <div class="ptop1">宠物名称:<span class="spanlv">*</span></div>
             <div class="ptop2">
                 <asp:TextBox ID="Tbpetname" runat="server" CssClass="fombox"></asp:TextBox>
                 <span class="spanlv">不要超过20个字</span>
             </div>
             </div>