Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 1 | # [Bootstrap MaxLength](http://mimo84.github.com/bootstrap-maxlength/) [](https://travis-ci.org/mimo84/bootstrap-maxlength) [](https://sourcegraph.com/github.com/mimo84/bootstrap-maxlength) |
| 2 | |||
| 3 | |||
| 4 | This plugin integrates by default with Twitter bootstrap using badges to display the maximum length of the field where the user is inserting text. |
||
| 5 | This plugin uses the HTML5 attribute "maxlength" to work. |
||
| 6 | |||
| 7 | |||
| 8 | The indicator badge show up on focusing on the element, and disappear when the focus is lost. |
||
| 9 | |||
| 10 | ## Configurable options |
||
| 11 | |||
| 12 | * **alwaysShow**: if true the threshold will be ignored and the remaining length indication will be always showing up while typing or on focus on the input. Default: false. |
||
| 13 | * **threshold**: this is a number indicating how many chars are left to start displaying the indications. Default: 10. |
||
| 14 | * **warningClass**: it's the class of the element with the indicator. By default is the bootstrap "label label-success" but can be changed to anything you'd like. |
||
| 15 | * **limitReachedClass**: it's the class the element gets when the limit is reached. Default is "label label-important label-danger" (to support Bootstrap 2 and 3). |
||
| 16 | * **separator**: represents the separator between the number of typed chars and total number of available chars. Default is "/". |
||
| 17 | * **preText**: is a string of text that can be outputted in front of the indicator. preText is empty by default. |
||
| 18 | * **postText**: is a string outputted after the indicator. postText is empty by default. |
||
| 19 | * **showMaxLength**: if false, will display just the number of typed characters, e.g. will not display the max length. Default: true. |
||
| 20 | * **showCharsTyped**: if false, will display just the remaining length, e.g. will display remaining lenght instead of number of typed characters. Default: true. |
||
| 21 | * **placement**: is a string, define where to output the counter. Possible values are: **bottom** ( *default option* ), **left**, **top**, **right**, **bottom-right**, **top-right**, **top-left**, **bottom-left** and **centered-right**. |
||
| 22 | * **message**: an alternative way to provide the message text, i.e. 'You have typed %charsTyped% chars, %charsRemaining% of %charsTotal% remaining'. %charsTyped%, %charsRemaining% and %charsTotal% will be replaced by the actual values. This overrides the options separator, preText, postText and showMaxLength. |
||
| 23 | * **utf8**: if true the input will count using utf8 bytesize/encoding. For example: the '£' character is counted as two characters. |
||
| 24 | * **twoCharLinebreak**: count linebreak as 2 characters to match IE/Chrome textarea validation. |
||
| 25 | * **customMaxAttribute**: allows a custom maxlength attribute to allow exceeding maxlength. 'overmax' class gets added when exceeded to allow user to implement form validation. |
||
| 26 | |||
| 27 | ## Examples |
||
| 28 | |||
| 29 | Basic implementation: |
||
| 30 | |||
| 31 | $('input[maxlength]').maxlength(); |
||
| 32 | |||
| 33 | Change the threshold value: |
||
| 34 | |||
| 35 | $('input.className').maxlength({ |
||
| 36 | threshold: 20 |
||
| 37 | }); |
||
| 38 | |||
| 39 | An example with some of the configurable options: |
||
| 40 | |||
| 41 | $('input.className').maxlength({ |
||
| 42 | alwaysShow: true, |
||
| 43 | threshold: 10, |
||
| 44 | warningClass: "label label-info", |
||
| 45 | limitReachedClass: "label label-warning", |
||
| 46 | placement: 'top', |
||
| 47 | preText: 'used ', |
||
| 48 | separator: ' of ', |
||
| 49 | postText: ' chars.' |
||
| 50 | }); |
||
| 51 | |||
| 52 | The same example using the message option: |
||
| 53 | |||
| 54 | $('input.className').maxlength({ |
||
| 55 | alwaysShow: true, |
||
| 56 | threshold: 10, |
||
| 57 | warningClass: "label label-info", |
||
| 58 | limitReachedClass: "label label-warning", |
||
| 59 | placement: 'top', |
||
| 60 | message: 'used %charsTyped% of %charsTotal% chars.' |
||
| 61 | }); |
||
| 62 | |||
| 63 | An example allowing user to enter over max characters. |
||
| 64 | Sample HTML element: |
||
| 65 | ```html |
||
| 66 | <textarea class="form-control" id="xyz" name="xyz" maxlength="10"></textarea> |
||
| 67 | ``` |
||
| 68 | |||
| 69 | // Setup maxlength |
||
| 70 | $('.form-control').maxlength({ |
||
| 71 | alwaysShow: true, |
||
| 72 | validate: false, |
||
| 73 | allowOverMax: true |
||
| 74 | }); |
||
| 75 | |||
| 76 | // validate form before submit |
||
| 77 | $('form').on('submit', function (e) { |
||
| 78 | $('.form-control').each( |
||
| 79 | function () { |
||
| 80 | if ($(this).hasClass('overmax')) { |
||
| 81 | alert('prevent submit here'); |
||
| 82 | e.preventDefault(); |
||
| 83 | return false; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | ); |
||
| 87 | }); |
||
| 88 | |||
| 89 | ## Changelog |
||
| 90 | |||
| 91 | ### 1.5.7 |
||
| 92 | * Fixed issue with bower |
||
| 93 | |||
| 94 | ### 1.5.6 |
||
| 95 | * Added over maxlength functionality with customMaxAttribute |
||
| 96 | * Added twoCharLinebreak option |
||
| 97 | |||
| 98 | ### 1.5.5 |
||
| 99 | * Implemented input event rather than keydown to improve usability |
||
| 100 | * Fixed jshint, jscs errors |
||
| 101 | |||
| 102 | ### 1.5.4 |
||
| 103 | |||
| 104 | * When an input with associated maxlength element is removed, maxlength is also removed. |
||
| 105 | |||
| 106 | ### 1.5.3 |
||
| 107 | |||
| 108 | * Fixed #40, error on resize event. |
||
| 109 | |||
| 110 | ### 1.5.2 |
||
| 111 | |||
| 112 | * Fixed #44 (pasted text in can cause it to go over the max length) |
||
| 113 | |||
| 114 | ### 1.5.1 |
||
| 115 | |||
| 116 | * Added self protection of multiple focus events |
||
| 117 | * Added back detection of window resizing |
||
| 118 | |||
| 119 | ### 1.5.0 |
||
| 120 | |||
| 121 | * Removed window.resize event |
||
| 122 | * Maxlength is created and destroyed each time |
||
| 123 | * Fixed Doesn't update the limit after input's maxlength attribute was changed [#31](https://github.com/mimo84/bootstrap-maxlength/issues/31) |
||
| 124 | * Added Gruntfile |
||
| 125 | * Added qunit unit tests |
||
| 126 | |||
| 127 | ### 1.4.2 |
||
| 128 | |||
| 129 | * Fixed issue with counting when the user moves with shift+tab keyboard shortcut. |
||
| 130 | * Replaced the warningClass limitReachedClass options to use labels rather than badges for Bootstrap 3.0 better compatibility. |
||
| 131 | |||
| 132 | ### 1.4.1 |
||
| 133 | |||
| 134 | * Added support for TAB key when the maxlength is reached. |