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

Is there a recommended way of restricting input length? #169

Open
ethagnawl opened this issue Mar 21, 2019 · 5 comments
Open

Is there a recommended way of restricting input length? #169

ethagnawl opened this issue Mar 21, 2019 · 5 comments
Projects

Comments

@ethagnawl
Copy link

First off, thanks for creating such a simple, useful plugin!

I find myself needing to limit the length of text a user may enter into the pell editor. I'm tempted to tack on an event listener and call preventDefault on the event object if the length of the pell editor's value is longer than N. Though, that seems like a kludge and like I'm working against the editor.

So, is there a recommended way of limiting the amount of text a user may enter into the pell editor? I'm looking at the source now and don't see any obvious solutions. Perhaps the event object could be passed to settings.onChange from within content.oninput?

@ethagnawl ethagnawl changed the title Is there a recommended of setting input max length? Is there a recommended of restricting input length? Mar 21, 2019
@simonfranzen
Copy link

simonfranzen commented May 29, 2019

I think you don't have to restrict input. What worked is just to show the remaining characters and when it's in a negative area you show a hint.

Something like this:

image

Look at this thread, it's quit interesting: https://ux.stackexchange.com/questions/13055/character-limits-on-fields-pros-and-cons-and-best-practices

UX Max Input length for pell

Append automatically the counter element after the pell container. I have a more complicated version implemented, but here is my approach.

Add a data-target and adata-max attribute to your pell container.

<div class="pell-init" data-target="#unique_target" data-max="500"></div>

in onChange of pell init

onChange: function(html) {
      
      var element = $(this.element);
      var target = element.data('target');
      var max = element.data('max');
      var counterEle = $('.text-counter[data-target="'+target+'"]');

      var lengthWithoutTags = $(html).text().length;
      var counter = parseInt(max) - lengthWithoutTags;
      
      // create counter element after pell container if not present
      if (!counterEle[0]){
        console.log("create new");
        counterEle = $('<div class="text-counter" data-target="'+target+'"></div>')
        element.after(counterEle);
      }
      
      if (parseInt(counter) < 0) {
        counterEle.html(`<span>Please use only ${max} characters.</span><span>${counter}</span>`);
        counterEle.addClass('text-counter--invalid');
      } else {
        counterEle.html(counter);
        counterEle.removeClass('text-counter--invalid');
      }
      return true; // dont know if needed
},

Give your text-counter-element a bit of styling

.text-counter
  font-size: 1rem
  width: 100%
  display: flex
  justify-content: flex-end

  & > *:last-child
    margin-left: auto

.text-counter--invalid
  color: red

Please notice: When counting characters you cant count the HTML tags inside. If you do this the user gets crazy numbers. For this I used this line $(html).text().length. It returns just the text and not the text including HTML tags. You also have to consider this in your backend validators.

@ethagnawl ethagnawl changed the title Is there a recommended of restricting input length? Is there a recommended way of restricting input length? May 29, 2019
@ethagnawl
Copy link
Author

Thanks, for following up @simonfranzen. That thread is an interesting read. I also wound up doing something similar to your approach in the application I was working on.

@siva-bathula
Copy link

I am also looking for something like this to be implemented. I want to restrict the user from typing more than a number of characters. I have gone through the comments here and seems like I will also have to do make do with a hack. I request the author to implement this as a feature or provide more hooks, so a preventDefault() maybe called on the original event.

@jaredreich
Copy link
Owner

jaredreich commented Oct 21, 2019

@siva-bathula This type of feature will likely never be a part of pell's core code, which is designed to be as minimal as possible. This makes more sense as a plugin.

@siva-bathula
Copy link

I will make do with a hack. Thanks for responding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
v2
Awaiting triage
Development

No branches or pull requests

4 participants