-3

I have an input field whose default value is already populated as 'Daisey12'.

If user tries to enter any value, it should not allow to enter any value. It should only allow delete and bakspace key so as to delete the value 'Daisey12'

Anyone knows if this is achievable in AngularJS?

Pochen
  • 2,871
  • 3
  • 22
  • 27
Adu Rao
  • 101
  • 2
  • 4
  • 10
  • Funny Jaromanda. :) I get you.. i just had to type something or else it would not allow me to post. tats y the tag in there. – Adu Rao Jan 05 '17 at 00:28
  • 3
    Rather than attempting to work around the rules built into the site's code, please follow them. As it stands, this question is off-topic, because you're asking for code, rather than presenting a specific problem with code and asking for the solution. See [How to Ask](https://stackoverflow.com/help/how-to-ask). – Nic Jan 05 '17 at 01:42
  • @QPaysTaxes- you are extra smart. rather than attempting to work around the rules build into the sites code ?? who build those rules. there is nothing like that. so please stop bragging about it. Check this link. http://stackoverflow.com/questions/459457/what-is-a-stored-procedure people have asked stupidest of questions like the one i mentioned above and yet have gotten 124 votes for that question. mine is a genuine working problem atleast. not a theoretical one. So, if you know the answer please help here. nobody wants to listen to the lecture which you gave above... thanks – Adu Rao Jan 05 '17 at 18:27
  • 2
    There's a difference between a question asking for a high-level description of a very important concept in a class of tools vital to a lot of programs, and a question asking for people to write code for you that explicitly admits to circumventing rules for the sake of posting a low-quality question. ***Read [How To Ask](https://stackoverflow.com/help/how-to-ask)*** – Nic Jan 05 '17 at 21:53
  • All that has been posted is a program description. However, we need you to [ask a question](//stackoverflow.com/help/how-to-ask). We can't be sure what you want from us. Please [edit] your post to include a valid question that we can answer. Reminder: make sure you know [what is on-topic here](//stackoverflow.com/help/on-topic), asking us to write the program for you and suggestions are off-topic. – 4castle Jan 05 '17 at 21:56

1 Answers1

2

In the link function of your directive you should wait on the keypress event and only allow to proceed if the pressed key is backspace or delete is pressed, otherwise prevent default:

link: function(scope, elem, attrs) {
        var limit = parseInt(attrs.limitTo);
        angular.element(elem).on("keypress", function(event) {
          if (event.keyCode === 8 || event.keyCode === 46) {
            event.preventDefault();
          }
        });
      }
Tome Pejoski
  • 1,582
  • 2
  • 10
  • 34
  • 3
    Please don't answer questions which don't at least roughly follow [these guidelines](https://stackoverflow.com/help/how-to-ask). Instead, help the poster improve their question, and as a 'reward', give them the answer they're looking for. That way, people ask better questions, which tend to lead to better answers, and everyone's happy. – Nic Jan 05 '17 at 01:43
  • I think you meant `&&` instead of `||` – 4castle Jan 05 '17 at 21:58
  • @4castle no `||` is fine. Since the code needs to prevent the default event behavior only if the pressed key is delete **or** backspace. – Tome Pejoski Jan 06 '17 at 00:08
  • In that case, why are you using `!==`? Currently your `if` statement is always true. – 4castle Jan 06 '17 at 02:00
  • @4castle you are right, thanks. I updated my answer. – Tome Pejoski Jan 06 '17 at 09:50