Mottie / Keyboard

Virtual Keyboard using jQuery ~
http://mottie.github.io/Keyboard/
Other
1.77k stars 722 forks source link

Prevent empty input #800

Closed GenijeOnline closed 3 years ago

GenijeOnline commented 3 years ago

Hello. I'm trying to prevent input of blank field but only at begining of text (blank + accept to enter text) and repeated space in text (example: "Text blank blank blank...+accept") on virtual keyboard. My idea is to implement something like this https://stackoverflow.com/questions/48714579/how-do-i-prevent-a-leading-space-from-being-entered-in-a-textbox but for now with no success. It's working on phisical keyboard but not on virtual. Can you please help me with this?

Mottie commented 3 years ago

Hi @GenijeOnline!

Try this code (demo):

$(function() {
  $('#keyboard').keyboard({
    beforeInsert: function(e, keyboard, el, textToAdd) {
      return keyboard.caret().start === 0 && textToAdd.trim() === ''
        ? ''
        : textToAdd;
    }
  });
});
GenijeOnline commented 3 years ago

Yes! That's it.. Thank you Mottie.