-2

i am trying to develop a children magazine website and making a form for children, in this form i am trying to take the input as (date) type and calculate the difference by years and return a message or alert if the age was 18=< age <= 6, now I've tried many codes that I found in this site or other sites as : 1. javascript - Age calculation ( i tried : getTime() and other codes ) 2. https://www.w3resource.com/javascript-exercises/javascript-date-exercise-18.php and many had the same ideas, but all of them didn't work ..

here is the code :

</head>
<body>
    <input type="date" id="userage" name="userage" onchange=""/>
    <p id="k" ></p>
    <script>
        var a = document.getElementById('k').value ; 
        var b = Date.parse(a) ;
        var c = new Date(b);
        var d = c.getFullYear();

        function useragefun(birthday){ 
            var e = new Date(); 
            var f = e.getFullYear(); 

            var g = Math.floor(f - birthday);

            if (g >= 18 || g <= 6 ){

                document.getElementById('k').innerHTML = "You better sign in the adults form"; 


            }else "" ;





         }



    </script>
</body>

// it could be this or an alert ..

saronstar
  • 1
  • 2
  • 1
    You're question isn't all that clear. what are you loading in var a? and why should Date parse it? – fedesc Mar 10 '19 at 10:11
  • 1
    you don't even call `useragefun` at all in your code – Jaromanda X Mar 10 '19 at 10:13
  • 1
    your script reads and parses element with id `k` - which is a `p` ... and `p` do not have a `value` – Jaromanda X Mar 10 '19 at 10:15
  • because the getelementbyid().value will return a string . so i prased it to a date to get the year . – saronstar Mar 10 '19 at 10:16
  • @JaromandaX sorry i forgot to add it in this code but i tried it before and it still didn't work . – saronstar Mar 10 '19 at 10:18
  • you forgot to add what? the ability for a `p` element to have a `value` - don't bother, it can not have a value, it is not an input element – Jaromanda X Mar 10 '19 at 10:20
  • @JaromandaX i called useragefun and changed

    to

    still didn't work .
    – saronstar Mar 10 '19 at 10:22
  • @saronstar a `
    ` still doesn't have a `value` property.
    – VLAZ Mar 10 '19 at 10:28
  • – saronstar Mar 10 '19 at 10:31
  • `document.getElementById('userage').nodeValue` why would you try to use `value` for an element that doesn't have one, but then when accessing an element that DOES have value, you try to use `nodeValue` instead ... it's like you're deliberately using the wrong properties - and furthermore, the input wont have a value on page load - which is when you're trying to get its value – Jaromanda X Mar 10 '19 at 10:41
  • 1
    and you STILL haven't shown how you intend on calling `useragefun` – Jaromanda X Mar 10 '19 at 10:42
  • @JaromandaX onchange="useragefun(c)" ,also both .value and .nodeValue return (NAN) – saronstar Mar 10 '19 at 10:46
  • `useragefun(c)` - what is `c`? – Jaromanda X Mar 10 '19 at 11:02
  • 18<= age <= 6 is mathematically impossible. It is impossible for something to be greater than or equal to 18 and less than. or equal to 6 at the same time. It seems you meant 6<= age <= 18 instead – jro Mar 10 '19 at 11:02
  • @jro OP's code logic suggests you need to *sign the adult form* if you are 18 or over, or 6 and under! so god knows what it really wants – Jaromanda X Mar 10 '19 at 11:04
  • @jaromanda-x Yep it's reflected in the question too. – jro Mar 10 '19 at 11:05
  • The question asks for mathematically impossible, the code suggests a child under six is an adult :p - i.e. the CODE can be true, but the *text* asks for the impossible – Jaromanda X Mar 10 '19 at 11:06

1 Answers1

0

Not really sure what you wanted, but this seems to be it:

        function useragefun(birthday){ 
               let birthDate = new Date(birthday),
         today = new Date(),
               years = (today.getFullYear() - birthDate.getFullYear());

    if (today.getMonth() < birthDate.getMonth() || 
        today.getMonth() == birthDate.getMonth() && today.getDate() < birthDate.getDate()) {
        years--;
    }
    if (years >= 18 || years <= 6 ){
                document.getElementById('k').innerHTML = "You better sign in the adults form"; 

            } else "";
         }
    <input type="date" id="userage" name="userage" onchange="useragefun(this.value)"/>
    <p id="k" ></p>
bru02
  • 360
  • 2
  • 10
  • it works , thank you very much . it was a great help , but can you explain to me why did you used : `if (today.getMonth() < birthDate.getMonth() || today.getMonth() == birthDate.getMonth() && today.getDate() < birthDate.getDate()) { years--; }` – saronstar Mar 10 '19 at 11:22
  • Because people are born on days other than today – Jaromanda X Mar 10 '19 at 12:10
  • i ment why `years--; ` :) – saronstar Mar 10 '19 at 12:43
  • because people have a birthday this year but after today ... if you were born on the 1st april 2001 you're not 18 yet ... herpa derp ... simple maths – Jaromanda X Mar 10 '19 at 23:54