-4

When you hold down a key (without a keyup event) the textbox looks something like

'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'.

i have a logic which goes into an infinite loop if one word (character string without blank) is >70 characters and so want to avoid the user typing this.

Any suggestions on how this can be achieved strictly through JS/jQuery?

(without using keyup event)

Su-J
  • 13
  • 1
  • 9
  • 1
    Check this http://www.w3schools.com/TAgs/att_input_maxlength.asp – 11thdimension Nov 15 '16 at 21:11
  • 1
    Sounds like you need to revisit your logic... – Greg Nov 15 '16 at 21:12
  • 1
    Use better logic. – Gavin Nov 15 '16 at 21:13
  • Can you upload a sample of the logic that breaks? Otherwise @11thdimension's suggestion about setting a max length or your text input – Pineda Nov 15 '16 at 21:14
  • This is a workaround to transfer data into already existing fields. the logic is pretty complicated for a beginner like me :) but i see a scope of completing it if this case is solved. – Su-J Nov 15 '16 at 21:15
  • @Pineda - its basically a logic for each line in a textbox. I split each line at spaces to avoid the number of characters in the line to be >70. and transfer the remaining characters to the next line on a keyup event. in this case, i have to limit the number of characters before the keyup event fires – Su-J Nov 15 '16 at 21:32
  • @Greg - the duplicate you mentioned uses a keyup event. i need to limit the number of characters before the keyup event is fired – Su-J Nov 15 '16 at 21:38
  • What do you have against the `keyup` event? – Matt Burland Nov 15 '16 at 21:40
  • And as others have say *"i have a logic which goes into an infinite loop if one word (character string without blank) is >70 characters"* it sounds like this is the part you ought to be fixing – Matt Burland Nov 15 '16 at 21:41
  • @MattBurland the keyup event divides the current line in the textbox into multiple lines by splitting at a space. since 'zzzzzzzzzzzzzzz' doesnt have a space, a string >70 is getting saved in another box with a max length 70.(this cannot be changed). neither can i divide 'zzzzzzzzzzz' at an arbitary point. – Su-J Nov 15 '16 at 21:45
  • So again - what do you have against `keyup`? You can attach more than one handler you know and the suggested duplicate has a workable solution. You set a flag on keydown and you clear it again on keyup. – Matt Burland Nov 15 '16 at 21:58

1 Answers1

1

The suggested dupe has a solution and there is no reason that you can't use it. You can attach more than one handler to the same event, so the fact that you have an existing keyup handler should not stop you:

var fired = false;

$("#box").keydown(function(e) {
  if (!fired) {
    fired = true;
  } else if (e.keyCode != 8) {  // 8 is the keyCode for backspace
    e.preventDefault();
  }
});

$("#box").keyup(function() {
  fired = false;
});

$("#box").keyup(function() {
  console.log("some other function that does something complicated...");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="box" />

As you can see here, this stops the repeating keys (although whether or not that's a good idea from the perspective of the user is a different question) and still executes the other keyup handler.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • though this prevents 'zzzzzzzzzzzzzz' ; it also prevents efficient typing... and navigation inside of the textbox. do you have suggestions to avoid that? – Su-J Nov 15 '16 at 22:21
  • @Su-J: Other than you can't hold down backspace, what "efficient typing" do you think is being prevented? You can always check what key is being pressed and not prevent repeats for backspace or delete, or any other key you want to allow repeats. – Matt Burland Nov 16 '16 at 15:29