JavaScript - Need Help Taking Date From Form Field And Checking If Over 18 Years Old
I am having some trouble with my code in that I am trying to take the value from the date of birth field and check to see if over 18 years old. I they are not over 18, a window.alert dialog box pops up to tell them. Any help with this I would greatly appreciate.
Below is the check if 18 validation. Script section in the document head: Code: //check if over 18 function overAge(){ var age; var input = document.forms[0].birthDate.value; var pyear = parseInt(input.substring(6,10)); var pmonth = parseInt(input.substring(0,2)) - 1; var pday = parseInt(input.substring(3,5)); if ( month < pmonth ){ age = year - pyear - 1; } else if ( month > pmonth ){ age = year - pyear; } else if ( month == pmonth ){ if ( day < pday ){ age = year - pyear - 1; } else if ( day > pday ){ age = year - pyear; } else if ( day == pday ){ age = year - pyear; } } if(age < 18){ window.alert('Attention: Under 18!'); valid=false; return valid; } } Code for body section: Code: Date of Birth: <br/> <input type = "text" name = "date" value = "(mm/dd/yyyy)"/> Similar TutorialsWhat I am wanting to do is have my form open up a new part of the form when the previous sections are filled out. i.e name field shows, when it has content, the next field opens up for it to be filled out, and so on. Hi folks. Looking to see if anyone could tell me where I'm going wrong with this script... I'm trying to have a field automatically filled with the day of the week ("Monday", "Tuesday", "Wednesday" and so on), upon the user selecting a date from the datepicker jQuery. If this isn't clear, I'll clarify further down the page. Here is the script: Code: <script type="text/javascript"> jQuery(document).ready(function($){ $('input[name="item_meta[428]"]').change(function(){ var d = $('input[name="item_meta[428]"]').val(); var n = new Date(d).getDay(); if(n == 0) val v = 'Sunday'; else if(n == 1) val v = 'Monday'; else if(n == 2) val v = 'Tuesday'; else if(n == 3) val v = 'Wednesday'; else if(n == 4) val v = 'Thursday'; else if(n == 5) val v = 'Friday'; else if(n == 6) val v = 'Saturday'; $('input[name="item_meta[429]"]').val(v).change(); }); }); </script> I'm basically trying to say, if the user selected today (15/05/2012) in the field 428 it would fill the field 429 with "Tuesday". Again, if the user selected 18/05/2012 in the field 428 then it would automatically fill field 429 with "Friday". It's being done to work in conjunction with a wordpress plugin called Formidable Pro hence the item_meta[428] etc. Any assistance would be greatly appreciated. Sam. Hi. I'm having some trouble with validating all my fields in my form. I'm posting my code here. Code: function validateForm() {var chk = document.forms["EntryPage"]["studentname"].value; if (chk==null || chksdtnm=="") { alert("Student name must be filled out"); return false; } if (chk==null || chksdtnm=="") { alert("Student ID must be filled out"); return false; } } I need to alert the user with a messagebox for six fields if ever they're not filled in. How do I achieve that? Okay, new to this magical thing called Javascript and LOVING it. Don't know how I avoided it all these years. Running into a stupid issue and I am sure it is just something I am missing. I have a hidden field in my page with the default value of HELLO. I am trying to make a change based on the default value, but I obviously am doing something wrong because it won't display my hidden field value. Any help would be greatly appreciated. Code: <html> <head> <script> function checkForm() { var testing = document.getElementById("testing").value; document.getElementById("result").innerHTML = testing; } </script> </head> <body> <input type="hidden" name="testing" value="HELLO" /> <input type="submit" value="submit" onClick="checkForm()"; /> <div id="result"></div> </body> </html> I have a problem checking that a date is in the past or future. Everything seems fine but the alert always comes back saying the date is in the past. Code: function OpenTimesheet(TSdate) { var SelectedDate = new Date(TSdate) if (SelectedDate > Date()) { alert (SelectedDate+' is a AFTER '+Date()) } else { alert (SelectedDate+' is a BEFORE '+Date()) } } TSdate is in yyyy/mm/dd format and looks fine, in both alerts the dates are written correctly, it just always says they are a 'BEFORE' date regardless. Any help with this one would be greatly apprieciated. I have a javascript that should convert any date into a number between 1 and 260. Based on the outcome of that calculation, a viewer is directed to a specific web page. How can I verify the calculation? How can I see what number javascript is arriving at so I can backtrack to the error? It is not serving up the correct page. I have compared the results to a calculator on another website that uses a different script to calculate the same result -- that's why I am saying the end results are not correct. On the other website, scroll down to where you enter a date. It returns a name just above the date (example: July 25, 1970 returns Electric Star). The script I included above returns Yellow Sun. July 25, 1970 should = 68. Code: function calculate() { if(!isValidDate()) return; var result = 0; var cuttingAge = 260; var yearBox = document.getElementById("year"); var year = yearBox.value; var month = document.getElementById("month").value; var day = document.getElementById("day").value; //Rule # 1 if(month==1 || month==2){ var selectedYearIndex; for(var i=0; i<yearBox.options.length; i++){ if(yearBox.options[i].selected){ selectedYearIndex = i; break; } } year = yearBox.options[selectedYearIndex-1].value; } //Rule # 2 var monthLength = new Array(); monthLength[0]=0; monthLength[1]=31; monthLength[2]=29; monthLength[3]=31; monthLength[4]=30; monthLength[5]=31; monthLength[6]=30; monthLength[7]=31; monthLength[8]=31; monthLength[9]=30; monthLength[10]=31; monthLength[11]=30; monthLength[12]=31; monthLength[13]=31; monthLength[14]=29; var dayPassed = 0; var tempMonth = month; if(month<3) //If its january or february tempMonth = parseInt(tempMonth) +12; //Add previous year's days for(var i=3; i<tempMonth; i++){ dayPassed += monthLength[i]; } dayPassed += parseInt(day) -1; dayPassed = dayPassed%cuttingAge; //Rule # 3 result = parseInt(year) + parseInt(dayPassed); //Rule # 4 if(result > cuttingAge) result -= cuttingAge; //Rule # 5 var pendant = new Array(); var abc = new Array(); pendant[0] = { name:"imix", values:new Array(1, 21, 41, 61, 81, 101, 121, 141, 161, 181, 201, 221, 241) }; pendant[1] = { name:"ik", values:new Array(2, 22, 42, 62, 82, 102, 122, 142, 162, 182, 202, 222, 242) }; pendant[2] = { name:"akbal", values:new Array(3, 23, 43, 63, 83, 103, 123, 143, 163, 183, 203, 223, 243) }; pendant[3] = { name:"kan", values:new Array(4, 24, 44, 64, 84, 104, 124, 144, 164, 184, 204, 224, 244) }; pendant[4] = { name:"chicchan", values:new Array(5, 25, 45, 65, 85, 105, 125, 145, 165, 185, 205, 225, 245) }; pendant[5] = { name:"cimi", values:new Array(6, 26, 46, 66, 86, 106, 126, 146, 166, 186, 206, 226, 246) }; pendant[6] = { name:"manik", values:new Array(7, 27, 47, 67, 87, 107, 127, 147, 167, 187, 207, 227, 247) }; pendant[7] = { name:"lamat", values:new Array(8, 28, 48, 68, 88, 108, 128, 148, 168, 188, 208, 228, 248) }; pendant[8] = { name:"muluk", values:new Array(9, 29, 49, 69, 89, 109, 129, 149, 169, 189, 209, 229, 249) }; pendant[9] = { name:"oc", values:new Array(10, 30, 50, 70, 90, 110, 130, 150, 170, 190, 210, 230, 250) }; pendant[10] = { name:"chuen", values:new Array(11, 31, 51, 71, 91, 111, 131, 151, 171, 191, 211, 231, 251) }; pendant[11] = { name:"eb", values:new Array(12, 32, 52, 72, 92, 112, 132, 152, 172, 192, 212, 232, 252) }; pendant[12] = { name:"ben", values:new Array(13, 33, 53, 73, 93, 113, 133, 153, 173, 193, 213, 233, 253) }; pendant[13] = { name:"ix", values:new Array(14, 34, 54, 74, 94, 114, 134, 154, 174, 194, 214, 234, 254) }; pendant[14] = { name:"men", values:new Array(15, 35, 55, 75, 95, 115, 135, 155, 175, 195, 215, 235, 255) }; pendant[15] = { name:"cib", values:new Array(16, 36, 56, 76, 96, 116, 136, 156, 176, 196, 216, 236, 256) }; pendant[16] = { name:"caban", values:new Array(17, 37, 57, 77, 97, 117, 137, 157, 177, 197, 217, 237, 257) }; pendant[17] = { name:"etznab", values:new Array(18, 38, 58, 78, 98, 118, 138, 158, 178, 198, 218, 238, 258) }; pendant[18] = { name:"cauac", values:new Array(19, 39, 59, 79, 99, 119, 139, 159, 179, 199, 219, 239, 259) }; pendant[19] = { name:"ahau", values:new Array(20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260) }; var redirecctPages = new Array(); redirecctPages["chuen"] = "http://store.mayankin.com/blue-monkey/"; redirecctPages["eb"] = "http://store.mayankin.com/yellow-human/"; redirecctPages["ben"] = "http://store.mayankin.com/red-skywalker/"; redirecctPages["ix"] = "http://store.mayankin.com/white-wizard/"; redirecctPages["men"] = "http://store.mayankin.com/blue-eagle/"; redirecctPages["cib"] = "http://store.mayankin.com/yellow-warrior/"; redirecctPages["caban"] = "http://store.mayankin.com/red-earth/"; redirecctPages["etznab"] = "http://store.mayankin.com/white-mirror/"; redirecctPages["cauac"] = "http://store.mayankin.com/blue-storm/"; redirecctPages["ahau"] = "http://store.mayankin.com/yellow-sun/"; redirecctPages["imix"] = "http://store.mayankin.com/red-dragon/"; redirecctPages["ik"] = "http://store.mayankin.com/white-wind/"; redirecctPages["akbal"] = "http://store.mayankin.com/blue-night/"; redirecctPages["kan"] = "http://store.mayankin.com/yellow-seed/"; redirecctPages["chicchan"] = "http://store.mayankin.com/red-serpent/"; redirecctPages["cimi"] = "http://store.mayankin.com/white-world-bridger/"; redirecctPages["manik"] = "http://store.mayankin.com/blue-hand/"; redirecctPages["lamat"] = "http://store.mayankin.com/yellow-star/"; redirecctPages["muluk"] = "http://store.mayankin.com/red-moon/"; redirecctPages["oc"] = "http://store.mayankin.com/white-dog/"; var pendantName; for(var i =0; i<pendant.length; i++){ var found = false; for(var j=0; j<pendant[i].values.length; j++){ if(result == pendant[i].values[j]){ pendantName = pendant[i].name; found = true; break; } } if(found) break; } document.location = redirecctPages[pendantName]; } /** * Comment */ function isValidDate() { var year = document.getElementById("year").value; var month = document.getElementById("month").value; var day = document.getElementById("day").value; if(month==2 && day>29){ alert("Please select a valid date"); return false } if((month==4 || month==6 || month==9 || month==11) && day>30){ alert("Please select a valid date"); return false } return true; } Hi folks, i am trying to generate a dynamic datefield with date mask "mm/dd/yyyy" and trying to insert it into Oracle db ...i still got the error ORA invalid month ehich means the date filed is not recognized as date: below is what i am doing : newStartDate = document.createElement( 'INPUT' ); newStartDate.setAttribute('type','Date'); newStartDate.setAttribute('id1','id'+ elementid+elementrow); newStartDate.setAttribute('name','StartDateName'+ elementid+elementrow); newStartDate.size=8; newStartDate.style.backgroundColor= bgc; any help thanks ?? Also i want to add a datepicke to this textbox..how it is posible / other option is to use Jquery datepicker but could not know how to impement it thanks again I am creating a website for someone and can get information from one form to check it so it can then change the data in another form. This what I have for JavaScript: <script type="text/javascript" language="JavaScript"> function price() { if(document.cust.top.value == None && document.cust.swirl.value == None) { document.price.hosted_button_id.value = "UDNNKUHTPGEEW"; } else { document.price.hosted_button_id.value = "CP8EVFLEASF28"; } } </script> The result I want is to take the value from my form that has a radio buttons and check to see if it equal none or not. Then using that information change the value in a different form. The first form looks like this for beginning: <form name="cust" method="get" action=""> Hi to all, I need some help checking a date field with javascript, I need to veryfy that the date introduced is not previous from today so when the submit button is clicked an alert comes up: "Date can't be previous from today" the date is compared with a PHP variable with de date from server "$todays_date" in 31/12/2011 format. Code: if (frm.date.value [is previous from] $todays_date) { alert('Date can't be previous from today') frm.date.focus() return false } Any help?? Thanks i need help with the following problem : I installed a new extension on my website , that allows a customer to select a delivery date, by displaying a table where he chooses a date. The script works perfectly but has 1 big downside . I can't make it a *required field . I am running a flower shop so delivery date is mandatory. anyways, i want to know if i can set it to mandatory modifying this code.. Code: <?php class MagentoTeam_Ddc_Block_Form extends Mage_Checkout_Block_Onepage_Abstract{ protected $delivery_date; protected $customer_comment; public function getDeliveryDate(){ if(is_null($this->delivery_date)){ $this->delivery_date = $this->getCheckout()->getQuote()->getDeliveryDate(); } return $this->delivery_date; } public function getCustomerComment(){ if(is_null($this->customer_comment)){ $this->customer_comment = $this->getCheckout()->getQuote()->getCustomerComment(); } return $this->customer_comment; } public function getConfigData($node){ return Mage::getStoreConfig(sprintf('magentoteam_ddc/%s', $node)); } public function getFirstDay(){ return intval(Mage::getStoreConfig('general/locale/firstday')); } } thank you in advance! I have a date of birth drop down for 'Day', 'Month' and a text field for Year. How can I get the select value of the drop down and year text field into a single text field, separated by '-'? The single text field would need to update if someone changed the drop down / year selections. Hope someone can help - I'm stumped! Hi All, I have a form that captures member registration details. In the admin section .. I would like to change the Status field of a record/s from NEW to PAYMENT DUE after 14 days from the data of submission. The status change should automatically trigger on the 15th day. So when the admin checks the list page he/she should be able to view the updated status field. Any pointers how to accomplish this? Thanks in advance. Vinny I've set up a mock registration form page so I can learn a bit about javascript's form validation. (newbie) I want to try to attempt to style the border of a form field green when the user enters the correct info into the form text field and red on all other fields if the user doesnt enter any info into them. When i test it, enter the right info into the username field, leave the others blank, and hit the submit button it styles the username field green ok but it doesnt make the next fields (password and so on) red. Could someone please explain what I am doing wrong? here is my code so far... Note: just for testing purposes I've put return false on everything so it displays a message when everythings ok. Code: .... <script type="text/javascript"> window.onload = function() { document.forms[0].username.focus(); } function validate(form) { var form = document.getElementById("reg"); var e = document.getElementById("error"); e.style.background = "red"; for(var i = 0; i < form.elements.length; i++) { var el = form.elements[i]; if(el.type == "text" || el.type == "password") { if(el.value == "") { e.innerHTML = "Please fill in all fields!"; el.style.border = "1px solid red"; el.focus(); return false; } else { el.style.border = "1px solid green"; return false; } } } var un = form.username; un.value = un.value.replace(/^\s+|\s+$/g,""); if((un.value.length < 3)|| (/[^a-z0-9\_]/gi.test(un.value))) { e.innerHTML = "Only letters,numbers and the underscore are allowed (no spaces) - 3-16 characters"; un.focus(); return false; } var pw = form.password; pw.value = pw.value.replace(/^\s+|\s+$/g,""); if((pw.value.length < 3) || (/[^a-z0-9]/gi.test(pw.value))) { e.innerHTML = "Only letters and numbers are allowed (no spaces) - 3-16 characters"; pw.focus(); return false; } e.innerHTML = "You filled in all the fields, well done!"; e.style.background = "green"; return false; } </script> </head> <body> <div id="wrapper"> <div id="header"> <h1>My Cool Website</h1> </div> <div id="content"> <div class="padding"> <h2>Registration</h2> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis ligula lorem, consequat eget, tristique nec, auctor quis, purus. Vivamus ut sem. Fusce aliquam nunc vitae purus. Aenean viverra malesuada libero. Fusce ac quam. Donec neque. Nunc venenatis enim nec quam. Cras faucibus, justo vel accumsan aliquam, tellus dui fringilla quam, in condimentum augue lorem non tellus. Pellentesque id arcu non sem placerat iaculis. Curabitur posuere, pede vitae lacinia accumsan, enim nibh elementum orci, ut volutpat eros sapien nec sapien. Suspendisse neque arcu, ultrices commodo, pellentesque sit amet, ultricies ut, ipsum. Mauris et eros eget erat dapibus mollis. Mauris laoreet posuere odio. Nam ipsum ligula, ullamcorper eu, fringilla at, lacinia ut, augue. Nullam nunc.</p> <form id="reg" action="#" method="post" onsubmit="return validate(this)"> <div id="error"></div> <div><label for="username">Username</label></div> <div><input type="text" name="username" id="username" size="30" maxlength="16" /></div> <div><label for="password">Password</label></div> <div><input type="password" name="password" id="password" size="30" maxlength="16" /></div> <div><label for="c_password">Confirm Password</label></div> <div><input type="password" name="c_password" id="c_password" size="30" maxlength="16" /></div> <div><label for="email">Email address</label></div> <div><input type="text" name="email" id="email" size="30" maxlength="200" /></div> <div><label for="c_email">Confirm Email address</label></div> <div><input type="text" name="c_email" id="c_email" size="30" maxlength="200" /></div> <div><label for="firstname">First name</label></div> <div><input type="text" name="firstname" id="firstname" size="30" maxlength="100" /></div> <div><label for="surname">Surname</label></div> <div><input type="text" name="surname" id="surname" size="30" maxlength="100" /></div> <div><label for="gender">Gender</label></div> <div> <div><input type="radio" name="gender" id="gender" value="m" checked="checked" />Male</div> <div><input type="radio" name="gender" value="f" />Female</div> </div> <div><input type="submit" value="Register" name="submit" /></div> </form> </div> </div> <div id="footer"> <p>Copyright © 2009 My Cool Website</p> </div> </div> </body> </html> I am trying to create a drop down menu for dates that starts with today's date, but allows people to choose 50 years into the past and 10 years into the future. Here's what I have for the years: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <h4>Select a date to see the energies for that day <script type="text/javascript"> var mesparaluna=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec']; function populatedropdown(mesparaluna, diaparaluna, axxa1){ var today=new Date() var mesparaluna=document.getElementById(mesparaluna) var diaparaluna=document.getElementById(diaparaluna) var axxa1=document.getElementById(axxa1) for (var i=0; i<31; i++) diaparaluna.options[i]=new Option(i, i+1) diaparaluna.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day for (var m=0; m<12; m++) mesparaluna.options[m]=new Option(mesparaluna[m], mesparaluna[m]+1) mesparaluna.options[today.getMonth()]=new Option(today.getMonth(), today.getMonth(), true, true) //select today's month var thisyear=today.getFullYear() for (var y=0; y<20; y++){ axxa1.options[y]=new Option(thisyear, thisyear) thisyear+=1 } axxa1.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year } </script> <script> sacakindias() </script> <SCRIPT SRC=tzolkin.js></script> <script> armatzolkin() </script> <p><font size="2" face="Verdana"> <SELECT class="input_tex" name=mesparaluna id="mesparaluna"></SELECT> </font> <SELECT class="input_tex" name="diaparaluna" id="diaparaluna"></SELECT> </font> <SELECT class="input_tex" name="axxa1" id="axxa1"></SELECT></p> <p><INPUT class="input_tex" onclick="sacakinpersonal()" type="button" value="Search"><img border="0" name="pirav" src="blanco.gif" class="shakeimage" width="10" height="10"> <INPUT class="input_tex" onclick="sacakindias()" type="button" value="Today"></p></form> <script type="text/javascript"> //populatedropdown(id_of_day_select, id_of_month_select, id_of_year_select) window.onload=function(){ populatedropdown("diaparaluna", "mesparaluna", "axxa1") } </script> </h4> </body> </html> I am also having a problem with the month. I wanted it to show the name of the month, but I am getting a number (and a wrong one at that). Apologies... I am a terrible newbie with Javascript. How can I add a form field value to a url. I tried the below but it no work. Code: href="index2.php?q=4&id=+document.theForm.lists.value+&picklist" I need to learn how to clear form fields when a user clicks on them. Here is the site in question: http://www.yourvancouvermortgagebroker.ca/apply They used to work, but then I changed the field value and now it doesn't work on some of the fields (whichever ones I changed). Here is a link to the .js file: http://www.yourvancouvermortgagebroker.ca/js/apply.js Hi there, I have a html form, and I need it so that when a user clicks a button, it adds a field into the form. I also need it to write <td></td> around the new field, and I need it to write this in a specific part row of a table. How would I go about doing this? I've tried searching for the past two hours, but no luck. Many thanks, I appreciate it, Jason Is there a way to give an input or textarea field the focus and set the focus to begin right at the end of the current text in the field?
function checkValidFormInput() { if (document.getElementsByName('customerName').value != '') { document.getElementById('customerNameImg').innerHTML = '<img alt="Valid" src="images/greenTick.png">'; } else { document.getElementById('customerNameImg').innerHTML = '<img alt="Invalid" src="images/redX.png">'; } if (document.getElementsByName('customerEmail').length > 0) { document.getElementById('customerEmailImg').innerHTML = '<img alt="Valid" src="images/greenTick.png">'; } else { document.getElementById('customerEmailImg').innerHTML = '<img alt="Invalid" src="images/redX.png">'; } } I have a form which has various fields which are arrays (so the name of a field may be myField[]). The reason for this is that the form has add buttons to add another field or fields for the purpose of entering another email address, etc., to send invitations and do other things. The problem that I am running into is how to access the individual fields in an array of fields inside javascript to do form validation. So if I have an email address field name emailAddress[], and using the add button, the user of my site adds 3 other copies of the emailAddress[] field, I want to then do form validation onSubmit to ensure those email address are valid using standard procedures. However, inside the javascript code, how do I access the individual fields that make up emailAddress[]? I just cannot find anywhere online the proper way to get at these values. If anyone can help me, I would greatly appreciate it. Happy Holidays |