liberapay.com icon indicating copy to clipboard operation
liberapay.com copied to clipboard

add character count for inputs and textareas

Open biskwikman opened this issue 1 year ago • 7 comments

Closes #2159

I'm not sure if this needs to also prevent the user from submitting if it's over the maxlength, as that attribute is dynamically removed.

biskwikman avatar Jul 10 '22 12:07 biskwikman

I'm not sure if this needs to also prevent the user from submitting if it's over the maxlength, as that attribute is dynamically removed.

The JavaScript code should enforce the length limit, yes. The Constraint Validation API is probably the solution.

Changaco avatar Jul 14 '22 16:07 Changaco

I reformatted the code. I think it's better and the selectors are more specific. I also implemented the constraint validation api. Something I'm not sure about is how to handle an invalid input. If the field is invalid, an error message is displayed when the user attempts to submit, but this would require some more text to be translated. I'm not sure how to do that from the Javascript code. We can also refine the regex constraint pattern more if needed. Right now it should allow a string of any characters, including whitespaces, from 1 to the max length. Just typing this out I'm realizing that I need to change that so optional input fields can accept no characters.

biskwikman avatar Aug 11 '22 10:08 biskwikman

I reformatted the code. I think it's better and the selectors are more specific.

I would prefer a single code block:

$('input[maxlength], textarea[maxlength]').each(function() {
    …
    $this.on('focus input', function() {
        …
    });
});

If the field is invalid, an error message is displayed when the user attempts to submit, but this would require some more text to be translated.

Since it's a static error message, it can easily be generated server side and transmitted to the browser as an HTML attribute, e.g. <input … data-maxlength-msg="{{ _('Maximum length is {0}.', …) }}">.

We can also refine the regex constraint pattern more if needed.

Why use a regexp at all? I suggest simply checking remainingLength < 0.

Changaco avatar Aug 11 '22 17:08 Changaco

I combined the code into a single block and removed the unnecessary regex.

As for the error message; will this have to be manually inserted into each instance of a relevant input or textarea element?

biskwikman avatar Aug 13 '22 10:08 biskwikman

Yes, the error message would have to be added to every relevant element.

Changaco avatar Aug 14 '22 09:08 Changaco

Ok, I'll start working on that soon.

biskwikman avatar Aug 14 '22 13:08 biskwikman

I added data-maxlength-msg="{{ _('Maximum length is {0}.', …) }}" to what I think are all of the relevant text elements. The password-form.html file also has a max length but I wasn't sure if this functionality would be appropriate there, so I didn't touch it.

There seems to be some issues when I input a string over the max length of an input field and then reload the page. For example, if you do this and then submit the form without interacting with the input fields again, it won't catch the error. I'm trying to figure out how to fix this.

biskwikman avatar Aug 16 '22 11:08 biskwikman