MiniSu
MiniSu

Reputation: 590

XSD date format required

In XSD i want to validate date format in MM/DD/YYYY using regex. Currently i am using following syntax which give me output in YYYY-DD-MM.

<xs:simpleType name="dateVal"> <xs:restriction base="xs:string"> <xs:pattern value="(19|20)((([02468][48]|[13579][26])-0?2-29)|\d\d-((0?[469]|11)-([012]?\d|3‌​0)|(l)-([012]?\d|3[01])|(0?2-([01]?\d|2[0-8]))))"></xs:pattern> </xs:restriction> </xs:simpleType>

Can any one help me to correct it for MM/DD/YYYY. Thank you in advance.

Upvotes: 1

Views: 1692

Answers (2)

Daniel B
Daniel B

Reputation: 8879

This regex pattern will match on the date format MM/DD/YYYY.

^([0]\d|[1][0-2])\/([0-2]\d|[3][0-1])\/([2][01]|[1][6-9])\d{2}(\s([0-1]\d|[2][0-3])(\:[0-5]\d){1,2})?$

Working example here.

As pointed out in the comments, this doesn't work as an XSD pattern. Removing the anchors and incorrect escaping of / characters result in:

([0]\d|[1][0-2])/([0-2]\d|[3][0-1])/([2][01]|[1][6-9])\d{2}(\s([0-1]\d|[2][0-3])(\:[0-5]\d){1,2})?

Also, take a look at this: Regular Expression Matching a Valid Date

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

Try this:

<xsd:simpleType name="Date">
   <xsd:restriction base="xsd:string">
     <xsd:pattern value="^(?:(?: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})$"/>
   </xsd:restriction>
</xsd:simpleType>

Upvotes: 2

Related Questions