![]() |
|
|
#1 |
|
Senior Member
Join Date: Aug 2011
Posts: 131
|
Dear friends,
here is a small but very usefull javascript based regex code to validate date in various formats Code:
<script type="text/javascript" language="javascript">
function isValidDate(prmDate) {
var re=/^(((0[1-9]|[12]\d|3[01])(\-|\/|\.)(0[13578]|1[02])(\-|\/|\.)((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)(\-|\/|\.)(0[13456789]|1[012])(\-|\/|\.)((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])(\-|\/|\.)02(\-|\/|\.)((19|[2-9]\d)\d{2}))|(29(\-|\/|\.)02(\-|\/|\.)((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/;
if (!re.test(prmDate)) {
return false;
} else {
return true;
}
}
</script>
Similarly for the date format MM/DD/YYYY and MM.DD.YYYY and MM-DD-YYYY, regex will be Code:
/^(((0[13578]|1[02])(\-|\/|\.)(0[1-9]|[12]\d|3[01])(\-|\/|\.)((19|[2-9]\d)\d{2}))|((0[13456789]|1[012])(\-|\/|\.)(0[1-9]|[12]\d|30)(\-|\/|\.)((19|[2-9]\d)\d{2}))|(02(\-|\/|\.)(0[1-9]|1\d|2[0-8])(\-|\/|\.)((19|[2-9]\d)\d{2}))|(02(\-|\/|\.)29(\-|\/|\.)((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/
Code:
/^((((19|[2-9]\d)\d{2}))(\-|\/|\.)(0[13578]|1[02])(\-|\/|\.)(0[1-9]|[12]\d|3[01])|(((19|[2-9]\d)\d{2}))(\-|\/|\.)(0[13456789]|1[012])(\-|\/|\.)(0[1-9]|[12]\d|30)|(((19|[2-9]\d)\d{2}))(\-|\/|\.)02(\-|\/|\.)(0[1-9]|1\d|2[0-8])|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))(\-|\/|\.)02(\-|\/|\.)29))$/
|
|
|
|
|
|
#2 |
|
Senior Member
Join Date: Aug 2011
Posts: 110
|
Very useful. such validation are very strict and accurate too.
Thank you for sharing. |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Aug 2011
Posts: 131
|
Thanks Kaman
|
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Aug 2011
Posts: 110
|
good sharing if you had more then please post
|
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Aug 2011
Posts: 103
|
to understand this pls describe some basics concepts
|
|
|
|
|
|
#6 |
|
Junior Member
Join Date: Sep 2011
Posts: 2
|
Hehe, it takes the whole book (or the whole website) to explain the concept of regular expressions. And the book is called "Mastering Regular Expressions" by Jeffrey Friedl.
Or you can read Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns (not parts, but the whole website!) |
|
|
|
|
|
#7 |
|
Junior Member
Join Date: Sep 2011
Posts: 2
|
everybody welcome to the forum about regular expressions with 700+ members.
http://groups.google.com/regex You can participate by email. |
|
|
|
|
|
#8 | |
|
Senior Member
Join Date: Aug 2011
Posts: 110
|
Quote:
|
|
|
|
|
|
|
#9 |
|
Senior Member
Join Date: Aug 2011
Posts: 110
|
yah previous link is good but second one is not in working
in first one there more information about regex. thanks for posting |
|
|
|
|
|
#10 |
|
Senior Member
Join Date: Aug 2011
Posts: 103
|
Thanks for your valuable information about rejex tutorial
|
|
|
|
![]() |
| Tags |
| regex validate date, regular expression |
| Thread Tools | |
| Display Modes | |
|
|