Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Input Checker Help

    What's supposed to happen is when you click the button, a prompt comes up asking the user how old they are, however nothing happens.

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    	<title>Age Checker</title>
    	<script>
    		function agechecker() {
    			var userAge = parseInt(prompt("How old are you?"),10);
    			if (isNaN(userAge() {
    				return prompt("Not a number. Refresh the page and try again");
    			}
    			else if (userAge < 18) {
    				window.location("http://reedom.net/young.html");
    			}
    			else {
    				window.location("http://reedom.net/old.html");
    			}
    		}
    	</script>
    </head>
    <body>
    	<button onclick="agechecker()">Click</button>
    </body>
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Did you try using your error console? Press F12 in any browser.

    Spot the error here:-

    if (isNaN(userAge() {

    Prompts are long obsolete as a way of getting user input.


    Quizmaster: How many players are there in a septet?
    Contestant: Well, September is the ninth month so it might be nine. I'll go for five.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder
    Join Date
    Aug 2012
    Posts
    122
    Thanks
    0
    Thanked 25 Times in 25 Posts
    Code:
    if (isNaN(userAge() {
    return prompt("Not a number. Refresh the page and try again");
    }
    You have userAge noted as a function, but above this line its a variable, so it looks like you made a typo by adding the extra '('.

    Below appears to make your code work:

    Code:
    <!DOCTYPE html>
    <!DOCTYPE html>
    <html>
    <head>
    	<title>Age Checker</title>
    	<script>
    		function agechecker() {
    			var userAge = parseInt(prompt("How old are you?"),10);
    			if (isNaN(userAge)) {
    				alert("Not a number.");
                                       agechecker();
    			}
    			else if (userAge < 18) {
    				window.location = "http://reedom.net/young.html";
    			}
    			else {
    				window.location = "http://reedom.net/old.html" ;
    			}
    		}
    	</script>
    </head>
    <body>
    	<button onclick="agechecker()">Click</button>
    </body>
    </html>
    hope this helps
    1 Corinthians 15:3-4 / Ephesians 2:8-9 - What or Who are you living for?

  • Users who have thanked Brandnew for this post:

    JamieVic (06-23-2013)

  • #4
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Everything seems to be working fine, however when you enter in letters or just clicking "Ok" or "Cancel", it loads the old page instead of returning an alert.

    Code:
    		function agechecker() {
    			var userAge = parseInt(prompt("How old are you?"),10);
    			if (userAge == isNaN) {
    				return alert("Not a number.");
    			}
    			else if (userAge < 18) {
    				location.assign("http://reedom.net/young.html");
    			}
    			else {
    				location.assign("http://reedom.net/old.html");
    			}
    		}

  • #5
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Brandnew View Post
    Code:
    if (isNaN(userAge() {
    return prompt("Not a number. Refresh the page and try again");
    }
    You have userAge noted as a function, but above this line its a variable, so it looks like you made a typo by adding the extra '('.

    Below appears to make your code work:

    Code:
    <!DOCTYPE html>
    <!DOCTYPE html>
    <html>
    <head>
    	<title>Age Checker</title>
    	<script>
    		function agechecker() {
    			var userAge = parseInt(prompt("How old are you?"),10);
    			if (isNaN(userAge)) {
    				alert("Not a number.");
                                       agechecker();
    			}
    			else if (userAge < 18) {
    				window.location = "http://reedom.net/young.html";
    			}
    			else {
    				window.location = "http://reedom.net/old.html" ;
    			}
    		}
    	</script>
    </head>
    <body>
    	<button onclick="agechecker()">Click</button>
    </body>
    </html>
    hope this helps
    Thanks, it's fixed now!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •