-1

I have mat input field in angular 6 in which i am going to restrict (.)dot on empty field. if value presents dot needs allowed otherwise if empty input field means it should throw error like "dot not allowed'

how to achieve this in angular 6

anyone help me please...

<mat-form-field floatLabel="never" class="full-width transparent-form">
    <input matInput name="hoursSpent" required #workedHoursValidation="ngModel"
           placeholder="Worked hours:e.g:1.5 (or) 1" class="w-100" appNumberAloneMask [form]="form"
           [ngModel]="newTaskHours" (input)="onSearchChange($event.target.value)" (ngModelChange)="newTaskHours = form.value.hoursSpent">
    <mat-error *ngIf="workedHoursValidation.hasError('required')">Required</mat-error>
</mat-form-field>
1

1 Answer 1

0

You can use pattern (regex expression) validation on your form, here is an example:

Validator that requires the control's value to match a regex pattern. This validator is also provided by default if you use the HTML5 pattern attribute.

const myForm = new FormGroup(
  {
    'hoursSpent': new FormControl(this.hero.name, [Validators.pattern('^[0-9]+\.?[0-9]*$')])
  }
);

You can find more information about angular validation here and specifically pattern validation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.