简体   繁体   中英

Validation pattern for date not working

I am having two textboxes like this :

<input class="searchDateFrom" pattern="((19|20)[0-9]{2})[\- \/.](0[1-9]|1[012])[\- \/.](0[1-9]|[12][0-9]|3[01]))" title="Invalid Date" type="text" placeholder="Search From" id="searchDateFrom"></input>
<input class="searchDateTo" type="text" placeholder="Search To" id="searchDateTo"></input>
<input class="Datesubmit" type="button" value="Search" id="Datesubmit"></input>

But the pattern yyyy/mm/dd is not working an it is accepting invalid date also.Please help

Also how to restrict that searchDateTo is greater than the searchDateFrom.Please help

Data validation in javascript is always complicate, and with regular expression is hard to take care about differnt months lenght, leap years and cultures.

You could make all simplier by introducing moment.js . With this tiny library you can validate date without worry abot dates, months length, leap years.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM