add validation for negative <td class="w-custom-10% rounded-none text-center">…
- User
add validation for negative
code<td class="w-custom-10% rounded-none text-center"> <ng-container *ngIf="country.selected"> <input min="0" onKeyPress="if(this.value.length==10) return false;" type="number" [(ngModel)]="country.population" required /> </ng-container> <ng-container *ngIf="!country.selected"> {{ country.population | number:'1.0':'en-US' }} </ng-container> </td> <td class="w-custom-10% rounded-none text-center"> <ng-container *ngIf="country.selected"> <input type="number" min="0" onKeyPress="if(this.value.length==10) return false;" [(ngModel)]="country.area" required /> </ng-container> <ng-container *ngIf="!country.selected"> {{ country.area | number:'1.0':'en-US' }} </ng-container> </td> - Assistant
To add validation for negative numbers, you can add a check in the input fields' ngIf conditions and use the min attribute to restrict the input to positive values. Here's an example of how you can modify the code:
html<td class="w-custom-10% rounded-none text-center"> <ng-container *ngIf="country.selected"> <input type="number" min="0" [(ngModel)]="country.population" required /> </ng-container> <ng-container *ngIf="!country.selected"> {{ country.population | number:'1.0':'en-US' }} </ng-container> </td> <td class="w-custom-10% rounded-none text-center"> <ng-container *ngIf="country.selected"> <input type="number" min="0" [(ngModel)]="country.area" required /> </ng-container> <ng-container *ngIf="!country.selected"> {{ country.area | number:'1.0':'en-US' }} </ng-container> </td> - User
add validation for negative dont put negative
code<td class="w-custom-10% rounded-none text-center"> <ng-container *ngIf="country.selected"> <input min="0" onKeyPress="if(this.value.length==10) return false;" type="number" [(ngModel)]="country.population" required /> </ng-container> <ng-container *ngIf="!country.selected"> {{ country.population | number:'1.0':'en-US' }} </ng-container> </td> <td class="w-custom-10% rounded-none text-center"> <ng-container *ngIf="country.selected"> <input type="number" min="0" onKeyPress="if(this.value.length==10) return false;" [(ngModel)]="country.area" required /> </ng-container> <ng-container *ngIf="!country.selected"> {{ country.area | number:'1.0':'en-US' }} </ng-container> </td> - Assistant
To add validation to prevent entering negative numbers, you can use the
patternattribute and a regular expression to only allow positive numbers. Here's an example of how you can modify the code:html<td class="w-custom-10% rounded-none text-center"> <ng-container *ngIf="country.selected"> <input type="number" min="0" pattern="[0-9]+" [(ngModel)]="country.population" required /> </ng-container> <ng-container *ngIf="!country.selected"> {{ country.population | number:'1.0':'en-US' }} </ng-container> </td> <td class="w-custom-10% rounded-none text-center"> <ng-container *ngIf="country.selected"> <input type="number" min="0" pattern="[0-9]+" [(ngModel)]="country.area" required /> </ng-container> <ng-container *ngIf="!country.selected"> {{ country.area | number:'1.0':'en-US' }} </ng-container> </td> - User
onKeyPress="if(this.value.length==10) return false;"
onkeypress="return event.charCode != 45"
i want to combine this
- Assistant
To combine the two validations to prevent entering negative numbers and limit the input length to 10 characters, you can modify the
onKeyPressevent like this:html<td class="w-custom-10% rounded-none text-center"> <ng-container *ngIf="country.selected"> <input type="number" min="0" pattern="[0-9]+" [(ngModel)]="country.population" required onKeyPress="return event.charCode != 45 && this.value.length < 10" /> </ng-container> <ng-container *ngIf="!country.selected"> {{ country.population | number:'1.0':'en-US' }} </ng-container> </td> <td class="w-custom-10% rounded-none text-center"> <ng-container *ngIf="country.selected"> <input type="number" min="0" pattern="[0-9]+" [(ngModel)]="country.area" required onKeyPress="return event.charCode != 45 && this.value.length < 10" /> </ng-container> <ng-container *ngIf="!country.selected"> {{ country.area | number:'1.0':'en-US' }} </ng-container> </td> - User
im getting 401 when im trying to do this addCountry(country: any): Observable { const headers = new HttpHeaders() .set('Content-Type', 'application/json'); return this.http.post(this.apiUrl, country, { headers }); }
maybe the cors is the problem
- Assistant
If you are receiving a 401 error when trying to make the
POSTrequest in youraddCountryfunction, it is likely not related to the code you provided. A 401 error typically indicates that the client (your frontend application) is not authorized to access the requested resource (the API endpoint).The issue could be related to the CORS (Cross-Origin Resource Sharing) policy implemented on the server. CORS is a security mechanism implemented by browsers to restrict cross-origin requests.
To resolve this issue, you can try the following solutions:
-
Check the server-side code: Make sure that the API endpoint allows requests from your frontend application's domain. The server needs to include the appropriate CORS headers in its response.
-