Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

isDate function - isolate for specific years

Posted on 2011-05-05
7
Medium Priority
?
584 Views
Last Modified: 2012-05-11
I'm using a javascript function that has a regular expression that checks for valid dates.  The regular expression works pretty well, howeer, one of the problems is that if a user makes a typo and enters a date way into the future it will cause an error.

For example, say, the user accidentally types in 5/5/9011.  That is actually a valid date, but it will cause an error.

So, I was wanting to modify the regular expression to isolate the years from say 1900 to 2100...two hundred years.

I'm not very good with regular expressions, so I was hoping some regex guru couild help with how to isolate those years in the following javascript function that uses the regex.

See the function below.

Thanks for any help.
function isDate(value)
{
   var dateRegEx = new RegExp(/^(?:(?:(?:0?[13578]|1[02])(\/|-)31)|(?:(?:0?[1,3-9]|1[0-2])(\/|-)(?:29|30)))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(?:(?:0?[1-9]|1[0-2])(\/|-)(?:0?[1-9]|1\d|2[0-8]))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(0?2(\/|-)29)(\/|-)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:\d\d)?(?:0[48]|[2468][048]|[13579][26]))$/);
 
   if (dateRegEx.test(value))
   {
      return true;
   }
   return false;
}

Open in new window

0
Comment
Question by:-Dman100-
7 Comments
 
LVL 23

Accepted Solution

by:
cmalakar earned 1000 total points
ID: 35698341
Try with attached
function isDate(value)
{
   var dateRegEx = new RegExp(/^(?:(?:(?:0?[13578]|1[02])(\/|-)31)|(?:(?:0?[1,3-9]|1[0-2])(\/|-)(?:29|30)))(\/|-)(?:[1][9]\d\d|[2][0]\d\d|2100)$|^(?:(?:0?[1-9]|1[0-2])(\/|-)(?:0?[1-9]|1\d|2[0-8]))(\/|-)(?:[1][9]\d\d|[2][0]\d\d|2100)$|^(0?2(\/|-)29)(\/|-)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:\d\d)?(?:0[48]|[2468][048]|[13579][26]))$/);
 
   if (dateRegEx.test(value))
   {
      return true;
   }
   return false;
}

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35698347

Something like this perhaps?
function isDate( value ) {
  var result = /^[01]?\d\/(([0-2]?\d{1})|([3][0,1]{1}))\/((19\d{2})|(20\d{2})|(2100))$/.test(value)
//  alert( result )
  return result
}

Open in new window

0
 
LVL 9

Expert Comment

by:Erdinç Güngör Çorbacı
ID: 35698360
This does the trick i guess
^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])[/._-](0[1-9]|1[0-2])[/._-](19[0-9][0-9]|20[0-9][0-9])$
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 41

Expert Comment

by:HonorGod
ID: 35698389
Please note, however, that this RegExp allows "invalid" dates, like Feb 31st...

For more detail about JavaScript dates, please take a look at this article:

http://www.experts-exchange.com/A_484.html
0
 
LVL 9

Expert Comment

by:Erdinç Güngör Çorbacı
ID: 35698391
sorry code view is here

 
^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])[/._-](0[1-9]|1[0-2])[/._-](19[0-9][0-9]|20[0-9][0-9])$

Open in new window


and if you wish to change restriction just change the part i ' underlined

^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])[/._-](0[1-9]|1[0-2])[/._-](19[0-9][0-9]|20[0-9][0-9])$
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35698424
Off-topic, but why not just return the value returned by "test()"?
function isDate(value)
{
   ...
 
   return dateRegEx.test(value);
}

Open in new window

0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 1000 total points
ID: 35702494
my suggestion would be to extend the capabilities of the function so that it can receive an optional parameter where you can specify the minimun and maximum constraints:
function isDate(value,o)
{
        //these are the default min/max constraints if nothing is specified.
	var opt={'min':'1/1/1900','max':'12/31/2100'};


	var dateRegEx = new RegExp(/^(?:(?:(?:0?[13578]|1[02])(\/|-)31)|(?:(?:0?[1,3-9]|1[0-2])(\/|-)(?:29|30)))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(?:(?:0?[1-9]|1[0-2])(\/|-)(?:0?[1-9]|1\d|2[0-8]))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(0?2(\/|-)29)(\/|-)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:\d\d)?(?:0[48]|[2468][048]|[13579][26]))$/);

	opt.min=o&&o.min&&dateRegEx.test(opt.min)?o.min:opt.min;
	opt.max=o&&o.max&&dateRegEx.test(opt.max)?o.max:opt.max;

	var OK=dateRegEx.test(value);
	if( OK && "undefined" !=typeof(opt.min))
	{
		OK=(Date.parse(value)>=Date.parse(opt.min));
	}
	if( OK && "undefined" !=typeof(opt.max))
	{
		OK=(Date.parse(value)<=Date.parse(opt.max));
	}
return OK;
}

//sample usage
alert( isDate('5/5/9011',{'min':'1/1/1900','max':'12/31/9100'}) )
alert( isDate('5/5/9011') )
alert( isDate('5/5/2011') )

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question