Search code examples
regexvalidationionic-frameworknumbers

Regex needed for IC number validation


I need to validate a 12 digit number. In which,

  • 1st & 2nd digits = less than 100
  • 3rd & 4th digits = between 1 and 12
  • 5th & 6th digits = between 1 and 31
  • And the remaining 6 digits can be any numbers from 0 to 9

    Example 190131958103

    Can anyone give me the regular expression which satisfies the above validation?


    Solution

  • A 2-digit value is less than 100 by definition (max value is 99), so you don't need to check for that. This regex will meet your other needs:

    ^\d{2}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])\d{6}$
    

    It starts with any 2 digits; then either 01-09 or 10-12; followed by one of 01-09, 10-19, 20-29 or 30-31; followed by 6 digits.

    Demo on regex101

    Update

    If you can't use \d in your pattern, replace it with [0-9] i.e.

    ^[0-9]{2}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])[0-9]{6}$