1

How do we strip out spaces at the end of the 10digit number when user copies it from place (like email or word docs etc) and pastes it in the search bar?

But this function is only working when we hit enter. I want blank spaces to be removed as soon as we paste number.

public onSearchPolicy( event: any){
 let search policy number= event.target?.value?.trim();
if ( searchPolicyNumber){
let searchPolicyObject = {
policy : searchPolicyNumber,}};
aria08
  • 21
  • 3
  • You can handle the `input` event of an input element. It fires when the value of the input element changes (thus it also fires when pasting something into the input element). –  May 13 '22 at 09:51
  • you can listen to the change and apply trim every change but its over killing and when you do want space user could not enter it. – dt170 May 13 '22 at 09:52

2 Answers2

0

You could apply your function when the search input change.

.html

<input (ngModelChange)="onSearchChange()" />

.ts

onSearchChange() {
    // Your code
}
cybering
  • 778
  • 7
  • 19
0

Great question, just keep it simple and:

  1. Get the element your pasting it in

document.getElementById or however your binding it in Angular (@ViewChild, etc).

  1. Then remove the spaces - yourElementText.trim()
jburtondev
  • 2,827
  • 1
  • 14
  • 20