-1

I ma new to angular ,In my application I need to validate a date and display span message based on validation .

Here I am using ngx-bootstrap datepicker .

HTML:

<label for="mDOB" class="control-label">Date of birth</label>
            <input [bsConfig]="bsConfig"  type="text" maxlength="10" name="mDOB" [(ngModel)]="mUser.mDOB" #mDOB="ngModel"
              placeholder="Date of birth" class="form-control" bsDatepicker required>
            <div class="help-block alert-danger col-sm-12" *ngIf="mDOB.errors?.required && mDOB.touched">
              * Date of Birth is required
            </div>

Here I have done the reguired field validation .it's working fine

Now I need to validate the date field accepts only numbers and format should be "mm/dd/yyyy".

app.component.ts

submitForm(newUser: User): void {

    //  pattern="^(0?[1-9]|1[0-2])/(0?[1-9]|1[0-9]|2[0-9]|3[01])/\d{4}$";
    // newUser.mDOB using this I can get the selected date .
    // it always returns 2018-06-11T18:30:00.000Z in this format 
    // So here I need to convert from this format to "mm/dd/yyyy"
  }

Can anyone help me to solve this .

Zhu
  • 3,679
  • 14
  • 45
  • 76
  • check here (https://stackoverflow.com/questions/46589865/date-and-currency-validation-in-angular-4)[link] – Hari Jun 29 '18 at 06:39

1 Answers1

0

what type of date validation do you want? just that the price is a valid date? in case you set the sort="date" at the enter element the browser will ensure that only a legitimate date is entered.

identical for your shown range validator and for any currency validator. you need to be able to set the type="wide variety" on the input detail and also you may not need a validator.

if you still do want to perform your very own validation, you could use ordinary expressions just as for your instance.

simply appearance up regular expressions for currency and date. for the date, something like this: javascript - regex to validate date layout

  • I read it already ,hope my post is different form this https://stackoverflow.com/questions/46589865/date-and-currency-validation-in-angular-4 @shahnawazraza – Zhu Jun 29 '18 at 06:49