Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Persian Date validation

I want a Regular Expression for validation Persian date like 1396/4/3, 1396/12/08 or something else.
in other words, I want to ensure that format of Persian Date (as String) is some thing like these valid formats are:

1.YYYY/MM/DD

2.YYYY/MM/D

3.YYYY/M/DD

4.YYYY/M/D


any Solution?

like image 755
Morteza Asadi Avatar asked Aug 21 '17 05:08

Morteza Asadi


3 Answers

use this Regex :

/^[1-4]\d{3}\/((0[1-6]\/((3[0-1])|([1-2][0-9])|(0[1-9])))|((1[0-2]|(0[7-9]))\/(30|([1-2][0-9])|(0[1-9]))))$/

this regex in jquery full check your persian date template

Year : 4 digits starts with 1300 or 1400

Month,Day : for the 6 first months of a year calculate 31 days and for the 5 next months of a year calculate 30 days and for last month of a year calculate 29 days

like image 164
Amir H KH Avatar answered Nov 09 '22 22:11

Amir H KH


This is a bit long but it works everywhere (for example in grep -E in bash):

^[1][1-4][0-9]{2}\/((0[1-6]\/(0[1-9]|[1-2][0-9]|3[0-1]))|(0[7-9]\/(0[1-9]|[1-2][0-9]|30))|(1[0-1]\/(0[1-9]|[1-2][0-9]|30))|(12\/(0[1-9]|[1-2][0-9])))

the format is: YYYY/MM/DD

from year 1100 to 1499

and Esfand is 29 days in this expression.

like image 41
William Valadilene Avatar answered Nov 10 '22 00:11

William Valadilene


use this regex:

^(\\d{4})/(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])$

with this regex:

  • Year is 4 digits.
  • Month is smaller than equal 12
  • Day is smaller than equal 31
like image 31
Morteza Asadi Avatar answered Nov 09 '22 23:11

Morteza Asadi