0

I need to write a code that send an alert when user insert letters instead numbers. The should anounce that letters are not accepted and invite the user to right input again.

What i tried:

<!DOCTYPE html>    
<html>    
<head>    
  <title>Calc the age </title>    
  <meta charset="utf-8">    
</head>    
    <title> Calc the age </title>    
    <script>            
        var name = prompt("Insert yout name:");    
        var born = parseInt(prompt("Insert your year born (with 4 numbers format):"));    
        var age = 2022 - born;

        while (born < 1922 || born > 2021 || born === NaN) {    
            alert("You didn't inserted your year born correctly!")    
            var born = parseInt(prompt("Insert your year born (with 4 numbers format):"));
        }     
        
        document.write("The year born of " + name + " is " + born + ". " + "In 2022 he'll have " + age + " years old.");    
    </script>    
</html>
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • https://stackoverflow.com/questions/175739/how-can-i-check-if-a-string-is-a-valid-number – epascarello Aug 31 '22 at 13:56
  • You should at least use `isNaN(born)`, because `NaN` is never equal to itself (since `NaN` is "Not a Number", but can be anything else really, and to things that are not numbers aren't necessarily equal either) – somethinghere Aug 31 '22 at 14:01
  • tks for give me help! I begin to knowing "isNan", following the epascarello and Cerbrus tip and i could know how to apply it, following somethinghere tip. Your advices was very important to me, guys!!! TKS! TKS! TKS! – Bruno França Aug 31 '22 at 18:56

0 Answers0