Aby vstupný text prijať číselné percent len Uhlovej

0

Otázka

Takže textu akceptovať len percent hodnoty môže byť trochu zložitejšie, tu je spôsob, ako to urobiť, že odstránením non číselné hodnoty a pridanie ' %' na konci.

neváhajte a podeľte sa o vaše nápady!

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

Najlepšiu odpoveď

0

aby písanie textu prijať float čísla a pridať "%" na konci:

  • v html súboru:
<input id="id" type="text" formControlName="percentControl" (focusin)="start($event)"(focusout)="end($event)" />
  • v .ts súbor:
  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();
  }
  • zaoberá prenos dát do a z back-end:
  1. Vysielanie dát:
 //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. Získavanie údajov:
 //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

V iných jazykoch

Táto stránka je v iných jazykoch

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