Age restriction using Javascript -
i new javascript absolutely little idea language. trying put age restriction in job application form date of birth text field, date format dd/mm/yyyy , applicants must @ between 15 , 80 years old @ time fill in form otherwise won't able apply. not want embed html file write in .js file only.
for dob input type text, name dob, id dob, pattern (0[1-9]|1[0-9]|2[0-9]|3[01])/(0[1-9]|1[012])/[0-9]{4}
thank you.
you can use min , max attributes of html5 input date
html:
<input type="date" id="txtdate" />
javascript :
var dttoday = new date(); var month = dttoday.getmonth() + 1; var day = dttoday.getdate(); var year = dttoday.getfullyear(); var maxyear = year - 18; if(month < 10) month = '0' + month.tostring(); if(day < 10) day = '0' + day.tostring(); var maxdate = maxyear + '-' + month + '-' + day; var minyear = year - 80; var mindate = minyear + '-' + month + '-' + day; alert(maxdate); document.queryselectorall("#txtdate")[0].setattribute("max",maxdate); document.queryselectorall("#txtdate")[0].setattribute("min",mindate);
Comments
Post a Comment