양식 제출 검증

3278 단어
   <form method="post" action="">
       <div class="int">
          <label for="username"> :</label>
          <input type="text" id="username" class="required"/>
       </div>
       <div class="int">
           <label for="username"> :</label>
           <input type="text" id="email" class="required"/>
       </div>
       <div class="int">
           <label for="username"> :</label>
           <input type="text" id="personinfo"/>
       </div>
       <div class="sub">
           <input type="submit" value=" " id="send"/>
           <input type="reset" id="res"/>
       </div>
   </form> 
          $(function(){
            $("form :input.required").each(function(){
                var $required = $("<strong class='high'>*</strong>");
                //$(this).parent().append($required);  // 
                $(this).parent().prepend($required);
            });
            $('form :input').blur(function(){
                var $parent = $(this).parent();
                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>');
                    }
                }
            });
            $("form :input").focus(function(){
                var $parent = $(this).parent();
                $parent.find(".formtips").remove();
            });
            $("#send").click(function(){
               var $parent = $(this).parent().parent();
               $parent.find(".formtips").remove();
               $("form .required:input").trigger('blur');
               var numError = $('form .onError').length;
               if(numError){
                  return false;
               }
            });
            $("#res").click(function(){
                var $parent = $(this).parent().parent();
                $parent.find(".formtips").remove();
            });
        });

좋은 웹페이지 즐겨찾기