TOA Posted August 19, 2011 Share Posted August 19, 2011 Hey guys, got a problem and I don't work much with javascript, so I'm stuck. I found a couple scripts online and merged them into this script for testing <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script> function checkAge(input) { var today = new Date(); var d = document.getElementById(input).value; if (d == "") { showMessage('empty',input); return false; } if (!checkFormat(input)) { // check valid format return false; } d = d.split("-"); var byr = parseInt(d[0]); var nowyear = today.getFullYear(); if (byr >= nowyear || byr < 1900) { // check valid year showMessage('impossible',input); return false; } var bmth = parseInt(d[1],10)-1; // radix 10! if (bmth <0 || bmth >11) { // check valid month 0-11 showMessage('impossible',input); return false; } var bdy = parseInt(d[2],10); // radix 10! var dim = daysInMonth(bmth+1,byr); if (bdy <1 || bdy > dim) { // check valid date according to month showMessage('impossible',input); return false; } var age = nowyear - byr; var nowmonth = today.getMonth(); var nowday = today.getDate(); var age_month = nowmonth - bmth; var age_day = nowday - bdy; if (age < 18 ) { showMessage('child',input); return false; } else if (age == 18 && age_month <= 0 && age_day <0) { showMessage('child',input); return false; } } function checkFormat(input) { var validformat = /\d{4}\-\d{2}\-\d{2}/; var d = document.getElementById(input).value; if (!validformat.test(d)) { // check valid format showMessage('format',input); return false; } return true; } function showMessage(messagetype, input) { if (messagetype == "format") { alert ("Invalid date format - please re-enter as YYYY-MM-DD"); } else if (messagetype == "impossible") { alert ("Impossible year/month/day of birth - please re-enter."); } else if (messagetype == "child") { alert ("Only adults are allowed."); } else if (messagetype == "empty") { alert ("Nothing was filled out."); } document.getElementById(input).select(); } function daysInMonth(month,year) { // months are 1-12 var dd = new Date(year, month, 0); return dd.getDate(); } </script> </head> <body> <form action="" method="POST" onsubmit="return checkAge('dob')"> <input type="text" name="dob" id="dob" value="" /> <input type="submit" value="submit" /> </form> </body> </html> Works great, love it. I tried to add it to the php script I need to run it in and it's like it's not even there. Here's the two scripts needed for the php stuff one setup script that is included in others // more html/php stuff here .... function outputHeader($title, $id, $ids, $desc) { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta name="description" content="<?php echo $desc; ?>" /> <title><?php echo $title ?></title> <script type="text/javascript"> function checkAge(input) { var today = new Date(); var d = document.getElementById(input).value; if (d == "") { showMessage('empty',input); return false; } if (!checkFormat(input)) { // check valid format return false; } d = d.split("-"); var byr = parseInt(d[0]); var nowyear = today.getFullYear(); if (byr >= nowyear || byr < 1900) { // check valid year showMessage('impossible',input); return false; } var bmth = parseInt(d[1],10)-1; // radix 10! if (bmth <0 || bmth >11) { // check valid month 0-11 showMessage('impossible',input); return false; } var bdy = parseInt(d[2],10); // radix 10! var dim = daysInMonth(bmth+1,byr); if (bdy <1 || bdy > dim) { // check valid date according to month showMessage('impossible',input); return false; } var age = nowyear - byr; var nowmonth = today.getMonth(); var nowday = today.getDate(); var age_month = nowmonth - bmth; var age_day = nowday - bdy; if (age < 18 ) { showMessage('child',input); return false; } else if (age == 18 && age_month <= 0 && age_day <0) { showMessage('child',input); return false; } } function checkFormat(input) { var validformat = /\d{4}\-\d{2}\-\d{2}/; var d = document.getElementById(input).value; if (!validformat.test(d)) { // check valid format showMessage('format',input); return false; } return true; } function showMessage(messagetype, input) { if (messagetype == "format") { alert ("Invalid date format - please re-enter as YYYY-MM-DD"); } else if (messagetype == "impossible") { alert ("Impossible year/month/day of birth - please re-enter."); } else if (messagetype == "child") { alert ("Only adults are allowed."); } else if (messagetype == "empty") { alert ("Nothing was filled out."); } document.getElementById(input).select(); } function daysInMonth(month,year) { // months are 1-12 var dd = new Date(year, month, 0); return dd.getDate(); } </script> <?php // more html/php stuff here .... } and this is how I call it <?php // more html/php stuff here /**** list everything out ****/ outputHeader($title, $id, $ids, ""); // included here // list columns echo "<p>To add/edit a family member (18 and older), include their information in the form below and click submit. <br />To remove them, click the remove link to the right of the family member you wish to remove.</p><p class='small'>*Note: \"Family members\" are defined as: spouse or partner, children - includes step and adopted children (must be over age 18), parents of employee and parents of spouse or partner.</p>"; if ($error == true) { $_SESSION['EnrollError'] = "Sorry, there was a system error. Please contact adminstrator."; } if (isset($_SESSION['EnrollError'])) { echo "<p id=\"err\">$_SESSION[EnrollError]</p>"; } echo "<form action='' method='POST' id='enrollment_form' onsubmit=\"return checkAge('DOB')\">\r\n"; // called here echo "<table id='enrollment_table'>\r\n"; //more html/php stuff here .... ?> I have no idea why it won't work inside the php script. I know that's alot of code, but I only posted what I thought would be relevant. Any help would be appreciated. Quote Link to comment Share on other sites More sharing options...
MarPlo Posted August 21, 2011 Share Posted August 21, 2011 Hy, Try add that JavaScript script into an external file, for example "script.htm", than apply readfile() into php code to include it (or include()). Like this: readfile('script.htm'); Quote Link to comment Share on other sites More sharing options...
TOA Posted August 22, 2011 Author Share Posted August 22, 2011 Thanks for the suggestion. What the problem was: I hadn't given the input an id. Now I've encountered a new problem though. I'll start a new post about it; it's a different problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.