winzip icon

Age Check Verify

Email
Submitted on: 1/2/2015 1:52:00 PM
By: Norberto Olazabal (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2)
Views: 27975
 
     Some web sites like tobacco industry require by federal law that you check the age of every visitor; this JavaScript code will ask your date of birth and then check to see if Are old enough to see the site. This my look s t u p i d but that what the feral law requires. This will create a cookie that will check on very page of the site if you have checked your age. I have collected lines of codes from many places to put this together and I thank every one for their code. This is intended for training purpose only.

 

HTML:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//HTML for :Age Check Verify
//**************************************
<head>
<title>agecheck.htm</title>
<script language="javascript">
<!--
var pos = document.cookie.indexOf("UnderAge=");
if (pos == false)
{
 location = "ageok.htm";
}
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
function isDate(day,month,year) {
 var today = new Date();
 year = ((!year) ? y2k(today.getYear()):year);
 month = ((!month) ? today.getMonth():month-1);
 if (!day) return false
 var test = new Date(year,month,day);
 if ( (y2k(test.getYear()) == year) &&
 (month == test.getMonth()) &&
 (day == test.getDate()) )
 return true;
 else
 return false
}
function getMonthLength(month,year,julianFlag)
{
 var ml;
 if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10||month==12)
 {ml = 31;}
 else {
 if(month==2) {
 ml = 28;
 if(!(year%4) && (julianFlag==1 || year%100 || !(year%400)))
 ml++;
 }
 else
 {ml = 30;}
 }
 return ml; 
}
function CheckAge(form)
{
 var myLimit = 21;
 
 // Get Current Date
 Today=new Date();
 var yd = Today.getYear();
 var md = Today.getMonth();
 var dd = Today.getDate();
 
 //check for year on case netscape browser
	if (yd < 1900) 
	{
	yd = yd + 1900;
}
 // Get Date from the form
 var yb = form.yb.value;
 var mb = form.mb.options[form.mb.selectedIndex].value;
 var db = form.db.options[form.db.selectedIndex].value;
 
 
 
 MNames=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
 "Oct","Nov","Dec");
 
 
 if((db == 0) || 
 (mb == 0) || 
 (yb == 0))
 {
 alert("You must enter a valid date");
 return;
 }
 else
 {
		if (isDate(db,mb,yb)==false) 
			{
		 alert("You must enter a valid date");
		 return;
		 }
 }
 
 
 
 // Month length 0->use calendar length
 var mLength = 0;
 // 0 if Gregorian, 1 is Julian
 var isJulian = 1;
 var ma=0;
 var ya=0;
 var da = dd-db;
 // This is the all-important day borrowing code.
 if(da<0)
 {
 md--;
 // Borrow months from the year if necesssary.
 if(md<1)
 {
	 yd--;
	 // Determine no. of months in year
	 if(mLength)
	 {md=md+parseInt(365/mLength);}
	 else
	 {md=md+12;}
 }
 if(mLength==0) // Use real month length if no fixed
 { // length is indicated - note that we add a leap day if necessary.
 ml=getMonthLength(md,yd,isJulian);
	 da=da+ml;
 } 
 // For this case, everything works like it did in elementary school.
 else
	 {da+=mLength;} // Use fixed month length
 }
 ma = md - mb;
 // Month borrowing code - borrows months from years.
 if(ma<0)
 {
 yd--;
 if(mLength!=0)
	 {ma=ma+parseInt(365/mLength);}
 else
	 {ma=ma+12;}
 }
 ya = yd - yb;
 // Find if you are over AgeLimit.
 if(ya>=myLimit)
 {
 makeCookie("UnderAge",false);
 location = "ageok.htm"; 
 }
 else
 {
 alert("Sorry, you are not allowed to view this site.");
 }
 }
//Cookie things
function makeCookie(cookieName,cookieData)
{
 var expiry = new Date();
 expiry.setTime(expiry.getTime() + 1 * (24 * 60 * 60 * 1000));
 setCookie(cookieName, cookieData, expiry);
}
function setCookie(cookieName, cookieData, expiry)
{
 document.cookie = cookieName + "=" + escape(cookieData) + "; expires=" + expiry.toGMTString();;
}
//-->
</SCRIPT>
</HEAD>
<body> 
<FORM METHOD="post">
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TR><TH></TH><TH>Day</TH><TH>Month</TH><TH>Year
 
</TH></TR>
 <TR>
 <TD>Birthdate:  </TD>
 <TD align=middle><SELECT style="LEFT: -1px; TOP: 0px" name=db> <OPTION 
 value=0 selected>[Day]<OPTION value=1>1<OPTION value=2>2<OPTION 
 value=3>3<OPTION value=4>4<OPTION value=5>5<OPTION value=6>6<OPTION 
 value=7>7<OPTION value=8>8<OPTION value=9>9<OPTION value=10>10<OPTION 
 value=11>11<OPTION value=12>12<OPTION value=13>13<OPTION 
 value=14>14<OPTION value=15>15<OPTION value=16>16<OPTION 
 value=17>17<OPTION value=18>18<OPTION value=19>19<OPTION 
 value=20>20<OPTION value=21>21<OPTION value=22>22<OPTION 
 value=23>23<OPTION value=24>24<OPTION value=25>25<OPTION 
 value=26>26<OPTION value=27>27<OPTION value=28>28<OPTION 
 value=29>29<OPTION value=30>30<OPTION value=31>31</OPTION></SELECT></TD>
 <TD><SELECT size=1 name=mb> <OPTION value=0 selected>[Month]<OPTION 
 value=1>January<OPTION value=2>February<OPTION value=3>March<OPTION 
 value=4>April<OPTION value=5>May<OPTION value=6>June<OPTION 
 value=7>July<OPTION value=8>August<OPTION value=9>September<OPTION 
 value=10>October<OPTION value=11>November<OPTION 
 value=12>December</OPTION></SELECT></TD>
 <TD align=middle><SELECT name=yb> <OPTION value=0 selected>[Year]<OPTION 
 value=1920>1920<OPTION value=1921>1921<OPTION value=1922>1922<OPTION 
 value=1923>1923<OPTION value=1924>1924<OPTION value=1925>1925<OPTION 
 value=1926>1926<OPTION value=1927>1927<OPTION value=1928>1928<OPTION 
 value=1929>1929<OPTION value=1930>1930<OPTION value=1931>1931<OPTION 
 value=1932>1932<OPTION value=1933>1933<OPTION value=1934>1934<OPTION 
 value=1935>1935<OPTION value=1936>1936<OPTION value=1937>1937<OPTION 
 value=1938>1938<OPTION value=1939>1939<OPTION value=1940>1940<OPTION 
 value=1941>1941<OPTION value=1942>1942<OPTION value=1943>1943<OPTION 
 value=1944>1944<OPTION value=1945>1945<OPTION value=1946>1946<OPTION 
 value=1947>1947<OPTION value=1948>1948<OPTION value=1949>1949<OPTION 
 value=1950>1950<OPTION value=1951>1951<OPTION value=1952>1952<OPTION 
 value=1953>1953<OPTION value=1954>1954<OPTION value=1955>1955<OPTION 
 value=1956>1956<OPTION value=1957>1957<OPTION value=1958>1958<OPTION 
 value=1959>1959<OPTION value=1960>1960<OPTION value=1961>1961<OPTION 
 value=1962>1962<OPTION value=1963>1963<OPTION value=1964>1964<OPTION 
 value=1965>1965<OPTION value=1966>1966<OPTION value=1967>1967<OPTION 
 value=1968>1968<OPTION value=1969>1969<OPTION value=1970>1970<OPTION 
 value=1971>1971<OPTION value=1972>1972<OPTION value=1973>1973<OPTION 
 value=1974>1974<OPTION value=1975>1975<OPTION value=1976>1976<OPTION 
 value=1977>1977<OPTION value=1978>1978<OPTION value=1979>1979<OPTION 
 value=1980>1980<OPTION value=1981>1981<OPTION value=1982>1982<OPTION 
 value=1983>1983<OPTION value=1984>1984<OPTION value=1985>1985<OPTION 
 value=1986>1986<OPTION value=1987>1987<OPTION value=1988>1988<OPTION 
 value=1989>1989<OPTION value=1990>1990<OPTION value=1991>1991<OPTION 
 value=1992>1992<OPTION value=1993>1993<OPTION value=1994>1994<OPTION 
 value=1995>1995<OPTION value=1996>1996<OPTION value=1997>1997<OPTION 
 value=1998>1998<OPTION value=1999>1999</OPTION></SELECT></TD></TR>
<TR>
<TD align=middle colSpan=4>
 <P><BR><INPUT id=Enter onclick=CheckAge(this.form) type=button value="Enter Site" name=Enter></P></TD>
</TR>
</TABLE>
</FORM>
</body>
<html>
<head>
 <title>ageok.htm</title>
</head>
<SCRIPT LANGUAGE=javascript>
<!--
var pos = document.cookie.indexOf("UnderAge=");
if (pos != false)
{
 location = "agecheck.htm";
}
//-->
</SCRIPT>
<body marginwidth="0" marginheight="0" topmargin=0 leftmargin=0 >
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P align=center><FONT size=6 color=mediumslateblue><STRONG><U>Congratulations your really are 
getting old</U> 
</STRONG></FONT></P>
</body>
</html>
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.