Results 1 to 2 of 2
  1. #1
    TheSpookz is offline Member
    Join Date
    Apr 2016
    Rep Power

    Default While prompt answer is empty, or user pressed cancel?

    Hey Guys.
    I'm trying to make a website with an age restriction.
    When you log on you have to enter your name first, and for that I use a prompt window (Javascript), the problem here is that the user can easily get to the next line of code by simply pressing ''Cancel''.
    So I made this:
    while (userName === "" & userName === null); {
    userName = prompt("What's your name?")

    But after the user presses ''Cancel'' twice, the Javascript just stops entirely.
    Here's my entire code, including the HTML for the website. Ignore the HTML :)
    <!DOCTYPE html>
    <html lang="en-US">

    //Here I'll make a few variables for Javascript to work with.
    var accessGranted = false
    var userName
    var userNameConfirmed = false
    var userAge
    var userAgeConfirmed = false
    userName = prompt("What's your name?")
    //Now I'll make a while that has two conditions to run.
    while (userName === "" & userName === null); {
    userName = prompt("Hvad hedder du?")

    PLEASE HELP GUYS, I'm kinda new to Javascript... so bear over with me if I don't understand your comments :D

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: While prompt answer is empty, or user pressed cancel?

    Javascript <> Java.

    However you have a rogue semi-colon at the end of your while loop:
    Java Code:
    while (userName === "" & userName === null);
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. how can i create a string that show what key the user pressed?
    By I Lost My Name! in forum New To Java
    Replies: 2
    Last Post: 12-31-2013, 02:32 AM
  2. Replies: 2
    Last Post: 04-05-2012, 03:23 PM
  3. Replies: 1
    Last Post: 03-19-2012, 02:11 AM
  4. Compare true Answer /Player answer
    By Eilime in forum New To Java
    Replies: 1
    Last Post: 04-24-2011, 09:51 PM
  5. How should I cancel this thread ?
    By playwin2 in forum Threads and Synchronization
    Replies: 7
    Last Post: 08-27-2008, 07:20 PM

Posting Permissions

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