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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with Javascript Validation

    Hello!

    I'm putting together a very small project that requires the user to enter their age when they initially land on the homepage, via prompt. That age is then compared to a random number between 1 and 30; if their age is higher, they are directed to page A and if their age is lower, they are directed to page B.

    I have the prompt and redirections set up, but what I'm now trying to do is set up an alert so that if the user does NOT fill in their age, they are forced to do so before moving on to one of the two pages (A or B, depending). At the moment, the alert shows up when either the "Cancel" or "OK" button is clicked during the initial prompt, but I cannot get the prompt to reappear after clicking "OK" on the alert.

    My code is as follows:

    Code:
    $(window).load(function() {
    			
      var agePrompt=prompt("You should probably lie about your age:");
      var randomnumber=Math.floor(Math.random()*31)
    				
      if (agePrompt==null || agePrompt=="") {
        alert("You MUST lie about your age");
        return false;
      }
      else {
        if (agePrompt>=randomnumber) window.location.href="too-old.htm";
        else {
          window.location.href="take-a-seat.htm";
        }
      }
    });
    The actual page can be found here: http://philrules.com/17-tips/week-16/

    Any help would be greatly appreciated — even if you can just point me towards a resource!

    Thanks,

    Phil

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    prompt() returns a string so you need to convert it to a number before comparing it to randomnumber:

    Code:
    agePrompt = parseInt(agePrompt);
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    prompt() is a very primitive way of obtaining user input. But to avoid the ugly undefined in the prompt

    Code:
    var agePrompt=prompt("You should probably lie about your age:","");

    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.

  • #4
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help! I agree that the prompt() is not the best way to do it, so I set up a new version that should use text input to verify the age of the user. However, I'm not versed in script well enough to make it actually function.

    Here is what I have now: (also viewable at http://philrules.com/17-tips/week-16/)

    Code:
    $(window).load(function verify() {
      var ageNum=parseInt(document.forms["age-verification"]["age"].value);
      var randomNumber=Math.floor(Math.random()*31)
    				
      if (ageNum==null || ageNum=="") {
        return false;
        }
        else {
          if (ageNum>=randomNumber)window.location.href="too-old.htm";
          else {
            window.location.href="take-a-seat.htm";
          }
        }
      });
    The form I have set up to interact with this script is:

    Code:
    <form name="age-verification" onSubmit="verify()";>
    
    <label>Age</label>
    <input name="age" type="text" size="3" maxlength="2">
    
    <input name="Submit" type="Submit">
    </form>
    My apologies if this is all syntactically horrendous; I'm still learning the language.

    Best,

    Phil

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Code:
    <html>
    <head>
    </head>
    
    <body>
    
    <script type = "text/javascript">
    
    function verify() {
    var ageNum=parseInt(document.forms["age-verification"]["age"].value,10);//  if you use parseInt() youmust specify the radix
    var randomNumber=Math.floor(Math.random()*31);
    if (isNaN(ageNum)) {
    alert ("Please enter a numeric value for age");
    document.forms["age-verification"]["age"].value = "";
    return false;
    }
    
    if (ageNum>=randomNumber){window.location.href="too-old.htm"}
    else { window.location.href="take-a-seat.htm";  }
    
    }
    
    </script>
    
    <form name="age-verification" >
    
    <label>Age</label>
    <input name="age" type="text" size="3" maxlength="2">
    <input name="b1" type="button" value = "Verify" onclick = "verify()">
    </form>
    
    </body>
    </html>

    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.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Shoot, if most of us REAL coders here entered our REAL age, he wouldn't be sending any of us to take-a-seat.htm

    Now, if he made it a random number from 1 to 75, say, at least I'd have ALMOST an 11% chance.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Quote Originally Posted by Old Pedant View Post
    Shoot, if most of us REAL coders here entered our REAL age, he wouldn't be sending any of us to take-a-seat.htm

    Now, if he made it a random number from 1 to 75, say, at least I'd have ALMOST an 11% chance.
    I have the idea that if you entered your real age you would be (far) too old for him to be interested in you.

    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.


  •  

    Tags for this Thread

    Posting Permissions

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