Septanet Forum  

Go Back   Septanet Forum > Computers, Software, Harware, Information Technology > REGEX or Regular Expressions

Reply
 
LinkBack Thread Tools Display Modes
Old 16-08-2011, 11:02 AM   #1
Senior Member
 
sachin's Avatar
 
Join Date: Aug 2011
Posts: 131
Default Regular expression (REGEX) for date validation

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>
Above regular expression is valid for the date format DD/MM/YYYY and DD.MM.YYYY and DD-MM-YYYY

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))))$/
And for the date format YYYY/MM/DD and YYYY.MM.DD and YYYY-MM-DD, regex will be
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))$/
sachin is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 17-08-2011, 10:17 AM   #2
Senior Member
 
kaman's Avatar
 
Join Date: Aug 2011
Posts: 110
Thumbs up

Very useful. such validation are very strict and accurate too.
Thank you for sharing.
kaman is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 17-08-2011, 10:37 AM   #3
Senior Member
 
sachin's Avatar
 
Join Date: Aug 2011
Posts: 131
Default

Thanks Kaman
sachin is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 20-08-2011, 02:23 PM   #4
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Default

good sharing if you had more then please post
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 25-08-2011, 02:30 PM   #5
Senior Member
 
harry's Avatar
 
Join Date: Aug 2011
Posts: 103
Default

to understand this pls describe some basics concepts
harry is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 01-09-2011, 01:56 PM   #6
Junior Member
 
Join Date: Sep 2011
Posts: 2
Default

Quote:
Originally Posted by harry View Post
to understand this pls describe some basics concepts
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!)
accmailer is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 01-09-2011, 06:58 PM   #7
Junior Member
 
Join Date: Sep 2011
Posts: 2
Default forum about regular expressions

everybody welcome to the forum about regular expressions with 700+ members.
http://groups.google.com/regex
You can participate by email.
accmailer is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 02-09-2011, 10:16 AM   #8
Senior Member
 
kaman's Avatar
 
Join Date: Aug 2011
Posts: 110
Default

Quote:
Originally Posted by accmailer View Post
everybody welcome to the forum about regular expressions with 700+ members.
http://groups.google.com/regex
You can participate by email.
Thank you for the post. However, the link on google does not work.
kaman is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 02-09-2011, 10:32 AM   #9
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile

yah previous link is good but second one is not in working
in first one there more information about regex.
thanks for posting
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 02-09-2011, 11:12 AM   #10
Senior Member
 
harry's Avatar
 
Join Date: Aug 2011
Posts: 103
Default

Thanks for your valuable information about rejex tutorial
harry is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Reply

Tags
regex validate date, regular expression

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +5.5. The time now is 01:36 AM.


Content Relevant URLs by vBSEO 3.5.0 RC2