s2sh中jqury验证表默认提交

    技术2022-05-18  16

    表单文件:

     

     <div id="commentForm" >

     

     

         <strong>发表评论:</strong>

          <s:form action="/comment!addComment" method="post" name="commetnForm">

           <s:fielderror></s:fielderror>

          <s:hidden value="%{blog.id}" name="blog.id"></s:hidden>

          <table>

            <tr><td><input type="text" class="input" name="comment.name" id="name"/> 昵称<span>(必填)</span><label id="errorMsg"></label><td></td></tr>

            <tr><td colspan="2"><textarea name="comment.content" id="content"></textarea></td></tr>

            <tr><td><input type="submit" value="" class="sumbit" id="sub"/></td></tr>

     

          </table>

        </s:form>

     

        </div>

     

     

     

    js文件:

     

     

     

    $(document).ready(function(){

     

     commentName();

     commentContent();

     formbtn();

      

    })

     

     

    //验证表单提交 

     

     function formbtn(){

    $(":submit[id=sub]").click(function(check){  

            var name = $("#name").val();  

    var content = $("#content").val();

            if(name==""){  

                  var msg = "昵称不能为空" 

    // alert(msg)

      var span = "<span id = 'span'>"+msg+"</span>"

      $("#span").remove();

      $("#errorMsg").append(span);

                $("#name").focus();  

                check.preventDefault();//此处阻止提交表单  

            } else if(name.length > 20) {

       var msg = "昵称的字符不能超过20个字"

          var span = "<span id = 'span'>"+msg+"</span>"

           $("#span").remove();

           $("#errorMsg").append(span);

           $("#name").focus();  

                   check.preventDefault();//此处阻止提交表单  

    } else if (content ==  "") {

        var msg = "评论内容不能为空" 

                // alert(msg)

               var span = "<span id = 'span'>"+msg+"</span>"

                $("#span").remove();

                $("#errorMsg").append(span); 

    $("#content").focus();

    check.preventDefault(); 

    }

      

     

        });  

     

     }

    //验证评论人的昵称

     

    function commentName(){

     $("#name").blur(function(){

      var name = $("#name").val()

      

    // alert(name.length)

      var ss = name.length;

      if(name ==  "" ){

     var msg = "昵称不能为空" 

    // alert(msg)

    var span = "<span id = 'span'>"+msg+"</span>"

      $("#span").remove();

      $("#errorMsg").append(span);

        return false

    } else if(name.length > 20){

      var msg = "昵称的字符不能超过20个字"

       var span = "<span id = 'span'>"+msg+"</span>"

      $("#span").remove();

      $("#errorMsg").append(span);

      //alert(msg)

      return false

      }

      else {

       $("#span").remove();

     return true 

      }

      

      return true

      

      });

    }

     

     

    //验证评论内容

     

    function commentContent(){

    $("#content").blur(function(){

    var content = $("#content").val();

     

      if(content ==  "" ){

     var msg = "评论内容不能为空" 

    // alert(msg)

    var span = "<span id = 'span'>"+msg+"</span>"

      $("#span").remove();

      $("#errorMsg").append(span);

        return false

    } else {

       $("#span").remove();

     return true 

      }

      

             return true 

    });

    }


    最新回复(0)