Search code examples
javascriptangularangular-reactive-formsangular2-formsangular-validation

delete hyphen from an input field in angular 2 + reactive form


So, i just want remove when we delete data from input fields when we try to clear the input field then it's not going to clear because of i have added hyphen dynamically so is there a way to delete the or clear the input fields.

Basically i have added hyphen in the input field, but i am not able to delete hyphen when i trye to clear or deleting the data. please somebody help me out.

Here is my running code


Solution

  • You are already half way done all you need to:

    Replace

    if(event.keyCode === 8 || event.key === "Delete") return;
    

    With

    if(!event.data) return;