I am trying to validate this birthday form?
HTML
<div class="row">
<div class="col-md-4">
<div class="form-group">
<input type="text" class="form-control" id="usr" placeholder="Day">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<select class="form-control" id="sel1">
<option value="" disabled selected>Month</option>
<option>January</option>
<option>February</option>
<option>March</option>
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<input type="text" class="form-control" id="usr" placeholder="Year">
</div>
</div>
TypeScript
ngOnInit() {
this.SignupForm = new FormGroup({
'username': new FormControl(null, [Validators.required,
Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])'),
]),
'name': new FormControl(null, [Validators.required,
Validators.pattern('(?=.*[a-z])(?=.*[A-Z])'),
]),
'email': new FormControl(null, [Validators.required, Validators.email], this.forbiddenEmails),
'password': new FormControl(null, [Validators.required,
Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&].{8,}'),
]),
'phone': new FormControl(null, [Validators.required,
Validators.pattern('(?=.*[0-9])'),
]),
});
}
I am using a reactive form builder to validate other fields like Username, password... I also found this question here where was suggested to use moment.js but was confused. Can't figure out how to use moment js with this form for validating above mentioned birthday field with the moment.
You need to create a new form group for your birthday form, then you can validate each control: Day, Month, Year against a regular expression that ensures the combines value adheres to the format you desire (11/11/18) for example.
You don't need Moment.js unless you are trying to prepopulate a date, even then this isn't to do with validation, but rather the date it produces will need to be validated.