How to validate date in angular 4


How to validate date in angular 4



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>


* Date of Birth is required



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 .





check here (stackoverflow.com/questions/46589865/…
– N.HariHaraSudhan
2 days ago




1 Answer
1



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 stackoverflow.com/questions/46589865/… @shahnawazraza
– Zhu
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV