Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-19-2012, 03:13 PM   PM User | #1
philmccollam
New to the CF scene

 
Join Date: Sep 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
philmccollam is an unknown quantity at this point
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
philmccollam is offline   Reply With Quote
Old 09-19-2012, 03:25 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 09-19-2012, 03:29 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 09-19-2012, 08:15 PM   PM User | #4
philmccollam
New to the CF scene

 
Join Date: Sep 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
philmccollam is an unknown quantity at this point
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
philmccollam is offline   Reply With Quote
Old 09-19-2012, 08:56 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 09-20-2012, 07:04 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,193
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 09-20-2012, 08:13 AM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
alert, prompt, validation

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:46 PM.


Advertisement
Log in to turn off these ads.