Zrób tak, aby tekst wejściowy brał tylko liczbowe odsetki.

0

Pytanie

Zmusić wprowadzanie tekstu przyjmować tylko wartości procentowe może być nieco trudniejsze, oto sposób, aby to zrobić, usuwając jeśli nie podano wartości i dodając " % " na końcu.

zapraszam do dzielenia się swoimi pomysłami!

angular html regex typescript
2021-11-23 18:18:38
1

Najlepsza odpowiedź

0

aby wprowadzić tekst, zapoznać się z liczb zmiennoprzecinkowych i dodaj "%" na końcu:

  • w pliku html:
<input id="id" type="text" formControlName="percentControl" (focusin)="start($event)"(focusout)="end($event)" />
  • w pliku .ts:
  end(e) { 
    // console.log(/^[0-9.]*$/.test(e.target.value));
    if(!/^[0-9.]*$/.test(e.target.value))
        e.target.value = e.target.value.replaceAll(/[^0-9.]/g, '').trim();
   //add ' %' at the end
    if(e.target.value.length)
        e.target.value = e.target.value+ ' %';
    //this part is needed when working with angular form validation (ngForm required 
    //or formGroup Validators.required), else null value won't trigger the validation
    else 
        e.target.value = '0 %';
  }
  start(e) {
    e.target.value = e.target.value.replace('%', '').trim();
  }
  • praca z transmisją danych w wewnętrzną część i z powrotem:
  1. Umieszczenie danych:
 //make sure to get rid from ' %' when posting data to the backend
 //example with formControl
 // the + is for converting string to number
 dataToPost = +this.form.get('percentControl').value.replaceAll('%', '');

  1. Pobieranie danych:
 //Use Angular percent pipe
 local: string = "en-US";
 percentPipe:PercentPipe = new PercentPipe(this.local);
 myVariable = this.percentPipe.transform(dataFromBackEnd/100);

2021-11-23 18:18:38

W innych językach

Ta strona jest w innych językach

Русский
..................................................................................................................
Italiano
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................