JQuery验证字符数

时间:2011-02-16 08:18:55

标签: jquery validation jquery-validate

我正在使用一些jquery验证,并且它们都运行得非常好但是当用户输入的字符多于文本框中允许的字符数时,我希望验证消息说“您只允许介于10到200个字符之间。您输入了213个字符。

目前的代码如下:

rules: {
            Description: { rangelength: [10, 200] }
        },
        messages: {
            Description: "You are only allowed between 10 and 200 characters."
        }

有人有什么想法吗?

编辑:如果它保持编程的风格会很好,但如果不可能,我只需要找到另一种方法。

4 个答案:

答案 0 :(得分:3)

<html>
    <head>
        <title>Javascript</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
        <script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js" type="text/javascript"></script>



        <script>
            $(function(){
                $('#myform').validate({
                    rules: {
                        field: {
                            required: true,
                            rangelength: [2, 6]
                        }
                    },
                    messages: {
                        field: {
                            rangelength: function(range, input) {
                                return [
                                    'You are only allowed between ',
                                    range[0],
                                    'and ',
                                    range[1],
                                    ' You have typed ',
                                    $(input).val().length,
                                    ' characters'                                
                                ].join('');

                            }
                        }
                    }                      
                });
            });

        </script>
    </head>
    <body>
        <form id="myform">
            <input id="field" name="field" type="text"/>
            </br>
            <input type="submit" value="submit"/>
        </form>
    </body>
</html>

答案 1 :(得分:1)

void method1(){
      synchronized(this) {
           //...
      }
}

synchronized void method2(){
      synchronized(this) {
           //...
      }
}

你去!

答案 2 :(得分:0)

function LimitText(ref, iminlength, imaxlength) {
            if ((ref.value.length < iminlength) || (ref.value.length > imaxlength)) {
                alert("You are only allowed between " + iminlength + " and " + imaxlength + " characters\nCurrent length is: " + ref.value.length);
                ref.focus();
            }
        }


 <textarea name="myTextArea" rows="5" cols="5" onblur="LimitText(this,10,160)" ></textarea>

答案 3 :(得分:0)

我们使用这个小插件:

(function ($) {
  $.fn.textAreaLimit = function( limit, element ) {
    return this.each( function () {
      var $this = $(this);
      var displayCharactersLeft = function(charactersLeft) {
        if( element ) {
          $(element).html( (charactersLeft <= 0) ? '0' : charactersLeft );
        }
      };

      $this.bind('focus keypress blur click', function() {
        var val = $this.val();
        var length = val.length;
        if(length > limit) {
          $this.val( $this.val().substring(0, limit) );
        }
        displayCharactersLeft( limit-length );
      });

      displayCharactersLeft( limit-$this.val().length );
    });
  };
})(jQuery);

你可以像这样使用它:

$('textarea').textAreaLimit(500, ".chars-left");

元素.chars-left将更新为剩余字符数。当然,它适用于任何文本字段。