Home > Web Front-end > JS Tutorial > body text

基于jQuery实现表单提交验证_jquery

WBOY
Release: 2016-05-16 16:30:34
Original
1309 people have browsed it

html表单代码:

复制代码 代码如下:

  

      

         
         
      

      

          
          
      

      

          
          
      

      

          
          
      

  
 

jQuery代码:

复制代码 代码如下:

          $(function(){
            $("form :input.required").each(function(){
                var $required = $("*");
                //$(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                       var errorMsg = '请输入至少6位的用户名';
                      $parent.append(''+errorMsg+'');
                   }else{
                       var okMsg = '输入正确';
                       $parent.append(''+okMsg+'');
                   }
                }
                if($(this).is('#email')){
                    if(this.value==""||(this.value!=""&&!/.+@.+\.[a-zA-Z]{2,4}$/.test(this.value))){
                        var errorMsg = '请输入正确的E-mail地址';
                        $parent.append(''+errorMsg+'');
                    }else{
                        var okMsg = '输入正确';
                        $parent.append(''+okMsg+'');
                    }
                }
            });
            $("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();
            });
        });

代码很简单,也很易懂,小伙伴们直接拿走用吧,这里就不详细说明了。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!