Reputation: 77
I am trying to match an input field to be either MM/dd/yyyy, MM-dd-yyyy, or MMddyyyy.
I am a beginner to regular expressions but I wrote the following:
/^[0-9][0-9][\-\/0-9][0-9][0-9][\-\/0-9][0-9][0-9]([0-9][0-9])/
This matches both MM/dd/yyyy and MM-dd-yyyy but not MMddyyyy. It would also match just 10 numbers in a row which I don't want. Help would be greatly appreciated.
Upvotes: 0
Views: 10826
Reputation: 21
Using this regular expression in my project I found a little bug with the dates 10/29/YYYY and 10/30/YYYY. With the correction now it's working perfectly for me. The correct expression is:
ng-pattern='/^((0[13578]|1[02])[\/.](29|30|31)[\/.](18|19|20)[0-9]{2})|((01|0[3-9]|1[1-2])[\/.](29|30)[\/.](18|19|20)[0-9]{2})|((0[1-9]|1[0-2])[\/.](0[1-9]|1[0-9]|2[0-8])[\/.](18|19|20)[0-9]{2})|((02)[\/.]29[\/.](((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))$/'>
Upvotes: 0
Reputation: 69
Use the following ng-pattern to match the MM/DD/YYYY.Which will also validate the Leap year....
ng-pattern='/^((0[13578]|1[02])[\/.]31[\/.](18|19|20)[0-9]{2})|((01|0[3-9]|1[1-2])[\/.](29|30)[\/.](18|19|20)[0-9]{2})|((0[1-9]|1[0-2])[\/.](0[1-9]|1[0-9]|2[0-8])[\/.](18|19|20)[0-9]{2})|((02)[\/.]29[\/.](((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))$/
Upvotes: 2
Reputation: 77
So this code will work for what I want but it does not check for it being an actual date. For instance February 31st would be accepted.
/^([0-9]{2}[-/][0-9]{2}[-/][0-9]{4})|([0-9]{8})/
Upvotes: 1
Reputation: 122
Use the following ng-pattern
to match the MM/DD/YYYY
.
<md-datepicker flex
id="start"
label="start"
name="start"
ng-model="vm.start"
md-placeholder="Start Date"
ng-pattern="^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$"
required>
</md-datepicker>
Upvotes: 0