Jupp sobivat javascripti atribuutide kasutuseks
Allikas: Lambda
// ====== on load calls ===== function standardOnload(pagename) { initHighlight(); markFields(); showErrorMessages(); setValidationHandlers(); setMenuChangeHandler(); } function safeHTMLstr(str) { str=str.replace(/\\/g,' ') str=str.replace(/\'/g,' ') str=str.replace(/"/g,' ') return str } function initHighlight() { if (!document.getElementsByTagName){ return; } var allfields = document.getElementsByTagName("input"); // loop through all input tags and add events for (var i=0; i<allfields.length; i++){ var field = allfields[i]; if (((field.getAttribute("type") == "text") || (field.getAttribute("type") == "password")) && !field.getAttribute("readonly")) { field.onfocus = function () {this.style.backgroundColor = '#FFFF99';} field.onblur = function () {this.style.backgroundColor = '#FFFFFF';} } } var allfields = document.getElementsByTagName("textarea"); for (var i=0; i<allfields.length; i++){ var field = allfields[i]; if (!field.getAttribute("readonly")) { field.onfocus = function () {this.style.backgroundColor = '#FFFF99';} field.onblur = function () {this.style.backgroundColor = '#FFFFFF';} } } var allfields = document.getElementsByTagName("select"); for (var i=0; i<allfields.length; i++){ var field = allfields[i]; field.onfocus = function () {this.style.backgroundColor = '#FFFF99';} field.onblur = function () {this.style.backgroundColor = '#FFFFFF';} } } function setValidationHandlers() { if(!document.getElementsByTagName || !document.getElementById) return false; var formCollection = document.getElementsByTagName('input'); var formCollectionLength = formCollection.length; for (i=0; i<formCollectionLength; i++) { dt = formCollection[i].getAttribute('datatype'); if (dt == "date" || dt=="nofuturedate" || dt=="nopastdate") { formCollection[i].onkeydown = vld_kpv; } else if (dt == "estik") { formCollection[i].onkeyup = debug_ik; } else if (dt == "period") { formCollection[i].onkeydown = vld_period; } //mf = formCollection[i].getAttribute('mustfill'); } } function setMenuChangeHandler() { // kui dokus on olemas mainForm ja MenuNavigationForm siis pane viimasele // onSubmit() handler kylge mis kontrollib kas mainFormis on muudatusi // ja kui on siis kysib kas on vaja salvestada mainF = document.forms.mainForm; navForm = document.forms.MenuNavigationForm; if (!mainF || !navForm) return true; OnMenuNavigate = confirmChanged; } function markFields() { var e; try { for (var i=0;i<errFields.length;i++) { e=document.getElementsByName(errFields[i][0]); for(var j=0;j<e.length;j++) { if (j==0) e[j].focus(); e[j].style.backgroundColor = 'yellow'; } } } catch(err) { } } // ==== checking and errors ==== function fieldCheck(tablename) { // tablename is actually not used when calling! /* checks all the fields in the form, several special attributes are used that describe how field should behave. nicename - name of the field that is shown to user in error messages/other fialogs mustfill - element must have some kind of value condfill - element should have some kind of value, if not then we ask user if he really wants to continue datatype - type of the field date - date in DD.MM.YYYY format int - integer nonegint - positive integer estzip - estonian postal zip code estik - estonian ID code, tag that uses this should have also ref_bt_field and ref_gender_field that contain values of the birthday and gender fieldnames respectivelly email - email address (FIXME: implement) */ if (!document.getElementsByTagName){ return; } var htmlobs = new Array() var textfields = document.getElementsByTagName("input"); var areafields = document.getElementsByTagName("textarea"); var selectfields = document.getElementsByTagName("select"); htmlobs.push(textfields,areafields,selectfields); var tablenamepref; var field; var fname; var nfname; var tname; var notempty; var datatype; var condfill; var condfill_c = false; // when user tells us he wants to save even though some condfill fields are unfilled // then we set this to true so we wouldn't ask it again for each field var val; var condfld; if ('function' == typeof window.localPreFieldCheck) // localPreFieldCheck if it exists sets up data (like mustfills etc) if (!window.localPreFieldCheck()) // for this global fieldCheck(), should return true if ok return false; if (tablename!=null && tablename!='') tablenamepref=tablename+'_'; else tablenamepref=null; for (var j=0; j<htmlobs.length; j++) { for (var i=0; i<htmlobs[j].length; i++){ field=htmlobs[j][i] fname=field.name; if (tablenamepref!=null && fname!=null && fname.search(tablenamepref)!=0) { continue; } notempty=field.getAttribute('mustfill'); condfill=field.getAttribute('condfill'); //alert("Fname="+fname+" condfill="+condfill); datatype=field.getAttribute('datatype'); val=field.value; //alert(fname+'|'+notempty+'|'+datatype+'|'+val); if ((notempty=='yes' || condfill=='yes') && (val==null || val=='') && !condfill_c) { nfname=field.getAttribute('nicename'); if (nfname==null || nfname=='') { for(var l=0;l<document.getElementsByTagName('label').length;l++) {// mihkel added labels here if(document.getElementsByTagName('label')[l].htmlFor==field.id) { nfname=document.getElementsByTagName('label')[l].firstChild.nodeValue; break; } } if (nfname == null || nfname == '') nfname = fname; } //alert('VƤli '+nfname+' tuleb tƤita!'); if (condfill == 'yes') { // field is not absolutelly mandatory, we just must ask if user really wants to save data // when this is not filled condfld=document.getElementById('condfill_ok'); if (condfld && condfld!=null) { condfld.value='0'; } if (confirm('Kohustuslik vƤli '+nfname+' on tƤitmata. Kas oled kindel, et soovid salvestada?')) { condfill_c = true; } } else { alert('VƤli '+nfname+' tuleb tƤita!'); } if (!condfill_c) { // removed by tanel: && condfill field.onfocus = function () {this.style.backgroundColor = 'yellow';} field.focus(); return false; } } if (datatype=="date" && val.length > 0) { if (!checkDate(field)) { field.focus(); return false; } } else if (datatype=="nofuturedate" && val.length > 0) { if (!checkDate(field)) { field.focus(); return false; } } else if (datatype=="nopastdate" && val.length > 0) { if (!checkNopastDate(field)) { field.focus(); return false; } } else if ((datatype == "int" || datatype == "nonegint") && val.length > 0) { var intValue = parseInt(val, 10); nfname = field.getAttribute('nicename'); var strIbuf = ''+intValue; // length check is needed for cases like 28tere that will be converted to int as 28 if (isNaN(intValue) || (strIbuf.length != val.length)) { alert('VƤli '+nfname+' peab olema tƤisarv!'); field.focus(); return false; } if (datatype == "nonegint" && intValue < 0) { nfname = field.getAttribute('nicename'); alert('VƤli '+nfname+' peab olema mittenegatiivne tƤisarv!'); field.focus(); return false; } } else if (datatype == "estzip" && val.length > 0) { tmp_ival = parseInt(val, 10); nfname = field.getAttribute('nicename'); if (val.length != 5 || isNaN(tmp_ival)) { alert('VƤli '+nfname+' peab olema eesti postiindeksi formaadis (5 kohaline tƤisarv)'); field.focus(); return false; } } else if (datatype == "estik" && val.length > 0) { var birthdate_field_name = field.getAttribute('ref_bt_field'); var gender_field_name = field.getAttribute('ref_gender_field'); var bt_field = document.getElementsByName(birthdate_field_name)[0]; var gender_field = document.getElementsByName(gender_field_name)[0]; var ikcheck_retval = false; if (bt_field) { ikcheck_retval = check_isikukood(field, bt_field, gender_field, false) if (ikcheck_retval == false) return false } else { alert("Sul on defineeritud vƤli tĆ¼Ć¼biga estik. Defineeri sellele ka ref_bt_field ja ref_gender_field atribuudid, mis sisaldavad antud isikukoodiga seotud sĆ¼nniaja ja soo vƤljade nimesid, muidu ei saa automaatsed kontrollid tƤismahus toimida."); } } } } if ('function' == typeof window.localFieldCheck) { return window.localFieldCheck(); } else { return true; } } function showErrorMessages() { try { if (errorMessages!='') { if(errorMessages!='probleem') alert("Viga: "+errorMessages); return false; } else { return true; } } catch(err) { } } function check_isikukood(ik_field, birthdate_field, gender_field, mustfill) { var kood = ik_field.value; var koodNiceName = ik_field.getAttribute('nicename'); var res = false; var msg = ""; kood=trim(kood); if (kood.length == 0 && !mustfill) return true; if (kood.length != 11) { msg = koodNiceName + ": vale pikkus"; alert(msg); ik_field.focus(); return false; } var year = parseInt(kood.substring(1,3), 10); var monthS = kood.substring(3,5); var dayS = kood.substring(5,7); if (monthS.substring(0,1) == '0') { monthS = monthS.substring(1,2); } if (dayS.substring(0,1) == '0') { dayS = dayS.substring(1,2); } var month = parseInt(monthS, 10); var day = parseInt(dayS, 10); var century = parseInt(kood.substring(0,1), 10); //alert("yearS="+kood.substring(1,3)+" monthS="+monthS+" dayS="+dayS) //alert("year="+year+" month="+month+" day="+day); var ik_gender; if (isNaN(year) || isNaN(month) || isNaN(day) || isNaN(century)) { alert(koodNiceName + ": vale sugu vƵi sĆ¼nniaeg isikukoodis"); ik_field.focus(); return false; } if (century == 1 || century == 2) year = 1800 + year; else if (century == 3 || century == 4) year = 1900 + year; else if (century == 5 || century == 6) year = 2000 + year; else { msg = koodNiceName + ": vale sugu vƵi sĆ¼nniaja sajand"; alert(msg); ik_field.focus(); return false; } if (!checkDate2(day, month, year)) { msg = koodNiceName + ": vale sĆ¼nniaeg isikukoodis"; alert(msg); ik_field.focus(); return false; } else if (parseInt(kood.substring(10,11), 10) != kontrollsumma(kood.substring(0,10))) { msg = koodNiceName + ": vale kontrollsumma!"; alert(msg); ik_field.focus(); return false; } if (gender_field) { if (century % 2) ik_gender = 'M'; else ik_gender = 'N'; if (gender_field.value && gender_field.value != 'X' && gender_field.value != ik_gender) { msg = koodNiceName + ": sugu ei Ć¼hildu vƤljaga " + gender_field.getAttribute('nicename') ; alert(msg); gender_field.focus(); return false } else if (!gender_field.value) { gender_field.value = ik_gender; } } if (birthdate_field) { var ikISObdate = year.toString() + "-"; if (month < 10) ikISObdate += "0"; ikISObdate += month + "-"; if (day < 10) ikISObdate += "0"; ikISObdate += day; if (birthdate_field.value) { if (checkDate(birthdate_field)) { var ISObdtate = isoDateFromEstonian(birthdate_field.value); //alert(ikISObdate); //alert(ISObdtate); if (ikISObdate!= ISObdtate) { msg = koodNiceName + ": sĆ¼nniaeg ei Ć¼hildu vƤljaga " + birthdate_field.getAttribute('nicename') ; alert(msg); birthdate_field.focus(); return false } } } else { // birthday has no value birthdate_field.value = estonianDateFromISO(ikISObdate); } } // has birthday field attached return true; } function checkField(theField) { var val = theField.value; var niceName = theField.getAttribute('nicename'); if (!val || val.length== 0) { alert("Viga: " + niceName + " sisestamata"); return false; } return true; } function check_isikukood_or_regkood(isfyysiline, ik_field, birthdate_field, gender_field, mustFill) { if (isfyysiline) return check_isikukood(ik_field, birthdate_field, gender_field, mustFill); else { return checkRegkood(ik_field, mustFill); } } function checkRegkood(codeField, mustFill) { if (!mustFill && codeField.value.length == 0) { return true; } if (codeField.value.length != 8 || !isInteger(codeField.value)) { alert("Ć„riregistri kood peab olema 8 kohaline number"); return false; } return true; } var dtCh= "."; var minYear=1900; var maxYear=2100; function isInteger(s){ var i; for (i = 0; i < s.length; i++){ // Check that current character is number. var c = s.charAt(i); if (((c < "0") || (c > "9"))) return false; } // All characters are numbers. return true; } function stripString(s) { // strips leading and trailing whitespaces if (!s) return s; var i; var white = " \t\r\n"; var start = -1; var end = -1; var c; // Search through string's characters one by one. // If character is not in bag, append to returnString. for (i = 0; i < s.length; i++){ c = s.charAt(i); if (white.indexOf(c) == -1) { start = i; break; } } for (i=s.length; i >= 0; i--) { c = s.charAt(i); if (white.indexOf(c) == -1) { end = i+1; break; } } if (end > start && end > -1 && start > -1) return s.substring(start, end); else return ""; } function stripCharsInBag(s, bag){ var i; var returnString = ""; // Search through string's characters one by one. // If character is not in bag, append to returnString. for (i = 0; i < s.length; i++){ var c = s.charAt(i); if (bag.indexOf(c) == -1) returnString += c; } return returnString; } function daysInFebruary (year){ // February has 29 days in any year evenly divisible by four, // EXCEPT for centurial years which are not also divisible by 400. return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 ); } function DaysArray(n) { for (var i = 1; i <= n; i++) { this[i] = 31 if (i==4 || i==6 || i==9 || i==11) {this[i] = 30} if (i==2) {this[i] = 29} } return this } function checkDateOrDateInterval(dateField) { var dtStr = dateField.value; var nicename = dateField.getAttribute('nicename'); var pos1 = dtStr.indexOf("-"); var dtStr1; var dtStr2; if (pos1 >= 0) { dtStr1 = stripString(dtStr.substring(0,pos1)); dtStr2 = stripString(dtStr.substring(pos1+1)); //alert("Esimene pool on |"+ dtStr1 + "|"); //alert("Teine pool on |" + dtStr2 + "|"); var normalized1 = checkDateInternal(dtStr1, nicename, false, true); var normalized2 = checkDateInternal(dtStr2, nicename, false, true); if (!normalized1|| !normalized2) { alert(nicename + ": kuupƤevade vahemik peab olema kujul pp.kk.aaaa-pp.kk.aaaa"); return false; } var isoDate1 = isoDateFromEstonian(normalized1); var isoDate2 = isoDateFromEstonian(normalized2); if (isoDate2 < isoDate1) { alert(nicename + ": kuupƤeva vahemiku lƵppkuupƤev ei tohi olla vƤiksem kui alguskuupƤev"); return false; } if (normalized1 != dtStr1 || normalized2 != dtStr2) { var normalizedInterval = normalized1 + "-" + normalized2; dateField.value = normalizedInterval; } return true; } else return checkDate(dateField); } function checkDate(dateField) { var dtStr = dateField.value; var nicename = dateField.getAttribute('nicename'); // dtstr = value, so that we can call it return checkDateInternal(dtStr, nicename, dateField, true); // true = do alert } function checkNofutureDate(dateField) { var dtStr = dateField.value; var nicename = dateField.getAttribute('nicename'); var normalized1 = checkDateInternal(dtStr, nicename, dateField, true); if (!normalized1) { return false; } //alert('in checkNofutureDate from '+dtStr+' normalized1 '+normalized1) var isoDate1 = isoDateFromEstonian(normalized1); /* var today = new Date(); var tyear = today.getFullYear(); var tmonth = today.getMonth() + 1; if (tmonth<10) tmonth="0"+tmonth; var tday = today.getDate(); if (tday<10) tday="0"+tday; var curdate=tday+"."+tmonth+"."+tyear var normalized2 = checkDateInternal(curdate, nicename, false, true); */ var normalized2=document.getElementById('currentdate'); if (!normalized2 || normalized2=='' || normalized2==null) return normalized1; normalized2=normalized2.value; if (normalized2==null || normalized2=='') return normalized1; var isoDate2 = isoDateFromEstonian(normalized2); //alert('isoDate2 '+isoDate2+' isoDate1 '+isoDate1+' nicename '+nicename) if (isoDate2 < isoDate1) { alert(nicename + ": see kuupƤev ei tohi olla tulevikus"); return false; } else { //alert('normalized1 '+normalized1) return normalized1; } } function checkNopastDate(dateField) { var dtStr = dateField.value; var nicename = dateField.getAttribute('nicename'); var normalized1 = checkDateInternal(dtStr, nicename, dateField, true); if (!normalized1) { return false; } var isoDate1 = isoDateFromEstonian(normalized1); /* var today = new Date(); var tyear = today.getFullYear(); var tmonth = today.getMonth() + 1; if (tmonth<10) tmonth="0"+tmonth; var tday = today.getDate(); if (tday<10) tday="0"+tday; var curdate=tday+"."+tmonth+"."+tyear var normalized2 = checkDateInternal(curdate, nicename, false, true); */ var normalized2=document.getElementById('currentdate'); if (!normalized2 || normalized2=='' || normalized2==null) return normalized1; normalized2=normalized2.value; if (normalized2==null || normalized2=='') return normalized1; var isoDate2 = isoDateFromEstonian(normalized2); if (isoDate2 > isoDate1) { alert(nicename + ": see kuupƤev ei tohi olla minevikus"); return false; } else { return normalized1; } } function checkDateInternal(dtStr, nicename, dateField, doAlert) { var dtStr2 = dtStr; //alert(dtStr); //alert(nicename); var daysInMonth; var pos1,pos2; var strDay,strMonth,strYear; var month, day, year; var normalize = false; var normalizedDate; var datatype; if (dateField) { datatype=dateField.getAttribute('datatype'); if (!datatype || datatype=='') datatype="date"; } else { datatype="date"; } daysInMonth = DaysArray(12) if (!dtStr) return true; pos1=dtStr.indexOf(dtCh) if (pos1 >= 0) { pos2=dtStr.indexOf(dtCh,pos1+1) if (pos2 == -1) { if (doAlert) alert(nicename + ": kuupƤeva formaat peab olema : pp.kk.aaaa") return false } strDay =dtStr.substring(0,pos1); strMonth=dtStr.substring(pos1+1,pos2); strYear=dtStr.substring(pos2+1); if (strYear.length != 4 || strDay.length != 2 || strMonth.length != 2) normalize = true; } else if (dtStr.length == 6 || dtStr.length == 8) { normalize = true; strDay = dtStr.substring(0,2); strMonth =dtStr.substring(2,4); if (dtStr.length == 8) strYear = dtStr.substring(4,8); else strYear = dtStr.substring(4,6); } else { if (doAlert) alert(nicename + ": kuupƤeva formaat peab olemaa : pp.kk.aaaa"); return false; } var today = new Date(); var thisYear = today.getFullYear(); strYr = strYear; month = parseInt(strMonth, 10); day = parseInt(strDay, 10); year = parseInt(strYear, 10); if (strMonth.length<1 || month<1 || month>12){ if (doAlert) alert(nicename + ": kuu peab olema 1...12 aga on " + strMonth); return false; } if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){ if (doAlert) alert(nicename + ": pƤev ei vasta kuu pƤevade arvule"); return false; } if (year < 100) { if (datatype=='nopastdate' || (year < (thisYear - 2000) + 10)) year = 2000 + year; else year = 1900 + year; } if (year < minYear || year > maxYear){ if (doAlert) alert(nicename + ": aasta peab olema vahemikus "+minYear+" kuni "+maxYear) return false } if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){ if (doAlert) alert(nicename + ": sisesta Ƶige kuupƤev") return false; } if (normalize) { // if datefield given then will normalize normalizedDate = ""; if (day < 10) normalizedDate = "0"; normalizedDate = normalizedDate + day + "."; if (month < 10) normalizedDate += "0"; normalizedDate = normalizedDate + month + "." + year; if (dateField) dateField.value = normalizedDate; return normalizedDate; } return dtStr2 } function checkDate2(day, month, year) { var d = parseInt(day, 10); var m = parseInt(month, 10); var y = parseInt(year, 10); if (isNaN(d) || isNaN(m) || isNaN(y)) return false; if (m < 1 || m > 12) return false; return true; } function is_valid_range(aasta1,kuu1,paev1,aasta2,kuu2,paev2) // Kontrollida, et 1. kuupaev oleks varasem kui 2. kuupaev. // Teame, et kuupaevad ise on korrektsed (is_valid_date() on true). { if (aasta1 < aasta2) return true; if (aasta1 > aasta2) return false; // Aastad on samad // Kui siin parseInt ei pane, siis vordleb kui stringe ja 2 > 11 ! if (parseInt(kuu1) < parseInt(kuu2)) return true; if (parseInt(kuu1) > parseInt(kuu2)) return false; // Kuud on samad if (parseInt(paev1) < parseInt(paev2)) return true; return false; } function is_valid_fromto(kuupaev1, kuupaev2) // Kontrollida, et 1. kuupaev oleks varasem voi sama kui 2. kuupaev. // Teame, et kuupaevad ise on korrektsed (is_valid_date() on true). { arr = kuupaev1.split('.'); paev1 = parseInt(arr[0],10); kuu1 = parseInt(arr[1],10); aasta1 = parseInt(arr[2],10); arr = kuupaev2.split('.'); paev2 = parseInt(arr[0],10); kuu2 = parseInt(arr[1],10); aasta2 = parseInt(arr[2],10); return is_valid_range(aasta1,kuu1,paev1,aasta2,kuu2,paev2); } function keyhandler(e, keycodes, func) { // allows you to easily call some function // when one of the keys in the keycodes list is pressed // keycodes list should be string consisting of keynumbers separated by , // i.e. "38,12" // func should be a callable function // e is a event if(window.event) { // IE c = e.keyCode } else if(e.which) {// Netscape/Firefox/Opera c = e.which } var keyarray = keycodes.split(","); for (i=0; i<keyarray.length; i++) { if (c == parseInt(keyarray[i], 10)) { func(); return true; } } return true; } /* kontrollib, kas antud aasta, kuu ja pƤev sobivad kokku */ function kas_on_kpv(aasta,kuu,paev) { var kuud = new Array(31,29,31,30,31,30,31,31,30,31,30,31); rc = true; if(kuu != null && !isNaN(kuu)) rc &= kuu > 0 && kuu <= 12; if(paev != null && !isNaN(paev)) { rc &= paev > 0 && paev <= 31; if(kuu != null && !isNaN(kuu)) { rc &= paev <= kuud[kuu-1]; if(paev == 29 && kuu == 2) rc &= (aasta % 4 == 0 && (aasta % 100 != 0 || aasta % 400 == 0)); // liigaasta } } if(aasta != null && !isNaN(aasta)) { rc &= aasta >= 1000 && aasta <= 9999; } return rc; } function isoDateFromEstonian(vestDate) { // This function assumes normalized estonian date mm.dd.yyyy, 10 characters if (!vestDate || vestDate=="" || vestDate.length<10) return ""; else return vestDate.substring(6,10) + "-" + vestDate.substring(3,5) + "-" + vestDate.substring(0,2); } function estonianDateFromISO(visoDate) { // This function assumes normalized iso date 10 characters if (!visoDate || visoDate=="" || visoDate.length<10) return ""; else return visoDate.substring(8,10) + "." + visoDate.substring(5,7) + "." + visoDate.substring(0,4); } function isoDateDiffInDays(isoDate1, isoDate2) { //alert(parseInt(isoDate1.substring(0,4))-1900); var utc1 = Date.UTC(parseInt(isoDate1.substring(0,4), 10)-1900, parseInt(isoDate1.substring(5,7), 10) -1, parseInt(isoDate1.substring(8,10), 10), 1); var utc2 = Date.UTC(parseInt(isoDate2.substring(0,4), 10)-1900, parseInt(isoDate2.substring(5,7), 10) -1, parseInt(isoDate2.substring(8,10), 10), 1 ); //alert(utc1 + " " + utc2); var diff = ((utc1 - utc2) / 1000) / 3600 / 24 ; var res = parseInt(diff.toString(), 10); //alert(res); return res; } function isoCurrentDate() { var today = new Date(); var year = today.getYear(); var month = today.getMonth() + 1; var day = today.getDate(); if (year < 1900) year += 1900; var isoDate = year + "-"; if (month < 10) isoDate += "0"; isoDate += month + "-"; if (day < 10) isoDate += "0"; isoDate += day; return isoDate; } function estonianCurrentDate() { return estonianDateFromISO(isoCurrentDate()); } function ageFromISOBirthDate(birthdate) { // assume correct iso birthdate var today = new Date(); var tyear = today.getYear(); var tmonth = today.getMonth() + 1; var tday = today.getDate(); if (tyear < 1900) tyear += 1900; var byear = parseInt(birthdate.substring(0,4), 10); var bmonth = parseInt(birthdate.substring(5,7), 10); var bday = parseInt(birthdate.substring(8,10), 10); var age = tyear - byear - 1; if (tmonth > bmonth || (tmonth == bmonth && tday >= bday)) age++; return age; }