Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add character count for inputs and textareas #2162

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions js/10-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,40 @@ Liberapay.init = function() {
$('button[data-action="reload"]').on('click', function() {
location.reload();
});

// Add remaining length indicator on page load
$('input[maxlength], textarea[maxlength]').each(function() {
var $this = $(this)
var maxLength = $this.attr('maxlength');
var dataMaxlengthMsg = $this.data('maxlengthMsg');
$this.data('maxlength', maxLength);
$this.removeAttr('maxlength');
maxLength = $this.data('maxlength');
var remainingLength = maxLength - $this.val().length;
$this.after("<span class='remaining-length'>" + remainingLength + "</span>");

// Style remaining length indicator
$('.remaining-length').each(function() {
$(this).css({
'float': 'right',
'margin-right': '5px'
});
});

// Update remaining length dynamically
$this.on('focus input', function() {
var $this = $(this)
var maxLength = $this.data('maxlength');
var remainingLength = maxLength - $this.val().length;
$this.siblings("span[class='remaining-length']").first().text(remainingLength);

if (remainingLength < 0) {
$this.get(0).setCustomValidity(dataMaxlengthMsg);
} else {
$this.get(0).setCustomValidity('');
}
});
});
};

$(function(){ Liberapay.init(); });
Expand Down
1 change: 1 addition & 0 deletions www/%username/edit/statement.spt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ subhead = _("Descriptions")
<div class="form-group">
<input name="summary" class="form-control" size=60
maxlength="{{ constants.SUMMARY_MAX_SIZE }}"
data-maxlength-msg="{{ _('Maximum length is {0}.', constants.SUMMARY_MAX_SIZE) }}"
placeholder="{{ _('Short description in {language}', language=locale.Language(lang)) }}"
value="{{ summary }}" lang="{{ lang }}" />
</div>
Expand Down
2 changes: 2 additions & 0 deletions www/%username/edit/username.spt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ subhead = _("Username")
<input name="username" class="form-control"
value="{{ _username }}" required
maxlength="{{ constants.USERNAME_MAX_SIZE }}"
data-maxlength-msg="{{ _('Maximum length is {0}.', constants.USERNAME_MAX_SIZE) }}"
placeholder="{{ _('Username') }}"/>
<p class="help-block">{{
_("Maximum length is {0}.", constants.USERNAME_MAX_SIZE) + " " +
Expand All @@ -77,6 +78,7 @@ subhead = _("Username")
<input name="public_name" class="form-control"
value="{{ participant.public_name or '' }}"
maxlength="{{ constants.PUBLIC_NAME_MAX_SIZE }}"
data-maxlength-msg="{{ _('Maximum length is {0}.', constants.PUBLIC_NAME_MAX_SIZE) }}"
placeholder="{{ _('Name') }}"/>
<p class="help-block">{{
_("Maximum length is {0}.", constants.PUBLIC_NAME_MAX_SIZE) + " " +
Expand Down
4 changes: 3 additions & 1 deletion www/about/teams.spt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ title = _("Teams")
<input type="hidden" name="csrf_token" value="{{ csrf_token }}" />

<div class="form-group">
<input class="form-control" name="name" size=30 maxlength="{{ constants.USERNAME_MAX_SIZE }}"
<input class="form-control" name="name" size=30
maxlength="{{ constants.USERNAME_MAX_SIZE }}"
data-maxlength-msg="{{ _('Maximum length is {0}.', constants.USERNAME_MAX_SIZE) }}"
placeholder="{{ _('Name of the team') }}" />
</div>
<div class="form-group">
Expand Down
4 changes: 3 additions & 1 deletion www/for/%name/edit.spt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ title = _("{0} community settings", c.name)
"Short description in {language}", language=locale.Language(lang)
) }}</label>
<input class="form-control" name="subtitle" value="{{ subtitle or '' }}"
id="subtitle" maxlength="{{ c.subtitle_maxlength }}" lang="{{ lang }}" />
id="subtitle" maxlength="{{ c.subtitle_maxlength }}" lang="{{ lang }}"
data-maxlength-msg="{{ _('Maximum length is {0}.', c.subtitle_maxlength) }}" />
<p class="help-block">{{ _(
"Maximum length is {0}.", c.subtitle_maxlength
) }}</p>
Expand All @@ -110,6 +111,7 @@ title = _("{0} community settings", c.name)
) }}</label>
<textarea class="form-control vertical-resize" name="sidebar" rows=10
id="sidebar" maxlength="{{ c.sidebar_maxlength }}" lang="{{ lang }}"
data-maxlength-msg="{{ _('Maximum length is {0}.', c.sidebar_maxlength) }}"
>{{ sidebar or '' }}</textarea>
<p class="help-block">
{{ _("Maximum length is {0}.", c.sidebar_maxlength) }}
Expand Down
3 changes: 2 additions & 1 deletion www/for/new.spt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ title = _("Start a new community")
<label>
<span>{{ _("Name") }}</span>
<input class="form-control" name="name" value="{{ name }}"
required maxlength="{{ name_maxlength }}" />
required maxlength="{{ name_maxlength }}"
data-maxlength-msg="{{ _('Maximum length is {0}.', name_maxlength ) }}" />
<p class="help-block">{{ _(
"Use underscores (_) instead of spaces. All unicode alphanumeric "
"characters are allowed, as well as dots (.) and dashes (-)."
Expand Down