Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date of birth validation by using regular expression

My Question: I found this exercise very challenging. I'm kind stuck on the Date of Birth.

Challenge: Try to come up with the regular expressions to validate the following text strings (don't worry about case insensitivity):

  1. First name — should be composed of standard English letters and between one and ten characters in length.
  2. Middle initial — should be composed of standard English letters and be only one character in length.
  3. Last name — should be composed of standard English letters plus the apostrophe and between two and ten characters in length.
  4. Date of birth - should fall between 1/1/1900 and 12/31/2099, and should be one of the following date formats: dd/mm/yyyy, dd-mm-yyyy, or dd.mm.yyyy.

I was able to come up for the first three names. But I'm stuck on the Date of Birth.

   "^[a-z]{1,10}$",  // First name
   "^[a-z]$",        // Middle initial
   "^[a-z']{2,10}$", // Last name

Please help me.

like image 735
NinjaG Avatar asked Mar 03 '14 23:03

NinjaG


1 Answers

For regex for dates, see the link: Regex Tutorial

But I think the example will work.

  "^(0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])[-/.](19|20)\\d\\d$" 
like image 148
Jeff Avatar answered Nov 05 '22 12:11

Jeff