angularjs - validate md-datepicker date format as MM/DD/YYYY using a regular expression -
i tried validate md-datepicker date format mm/dd/yyyy using regular expression , md-error in following way
this regular expression accept mm/dd/yyyy format
const dob_regex = /^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/; this input-errors-example.ts file
import {component} '@angular/core'; import {formcontrol, validators} '@angular/forms'; const dob_regex = /^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/; @component({ selector: 'input-errors-example', templateurl: 'input-errors-example.html', styleurls: ['input-errors-example.css'], }) export class inputerrorsexample { dobformcontrol = new formcontrol(null, [ validators.required, validators.pattern(dob_regex) ]); } this html code snippet above
<form class="example-form"> <md-form-field class="example-full-width"> <input mdinput [mddatepicker]="picker" placeholder="choose date" [formcontrol]="dobformcontrol" onkeypress='return (event.charcode >= 48 && event.charcode <= 57) || event.charcode == 47' maxlength="10"> <md-datepicker-toggle mdsuffix [for]="picker"></md-datepicker-toggle> <md-datepicker #picker></md-datepicker> <md-error *ngif="dobformcontrol.haserror('required') || dobformcontrol.haserror('pattern')"> please enter valid date </md-error> </md-form-field> </form> this plunker relate problem.
this running usual, valid date inputs, once showing error message.
beside tried different regular expressions, same thing happen, there problem in approach or how overcome issue
that regex looks messy. think want:
/^(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d$/ give shot , should go. pretty clean read think. it's pretty hard coded range of 2 numbers, delimiter (which can change out), 2 more hard coded numbers, delimiter, , 4 numbers (1900-2999). can tweaked perhaps don't want 1900 example.
--update, response comment: there must odd how javascript being setup. here working python example:
import re def main(): theregex = re.compile("^(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d$") if re.search(theregex, "04/26/2017"): print ("match") main()
Comments
Post a Comment