4

I want to know how to alert "Oops, looks like you didn't enter anything", if the user has entered "Enter your name". i have an if and and elseif but i don't know if else would work for it. Here is my code

    <!DOCTYPE html>
    <html>
    <head>
    <title>Home</title>
    <link rel="stylesheet" type="text/css" href="style.css"/>
    </head>
    <body style="background-color: silver">
    <script>
        var name=prompt("Enter your name", "Enter your name")
        if (name !== '') {
            alert("Welcome" + " " + name)
        } else if(name !== false) {
            prompt("Oops, looks like you didn't enter anything");
        }
    </script>
    </body>
    <html>
Jsdodgers
  • 5,253
  • 2
  • 20
  • 36
Provision
  • 273
  • 1
  • 10

4 Answers4

1

This method defines a function getName() which takes the same arguments as prompt(). If no default value is specified, it uses the question that was passed to it. It then asks the user for their name using these values. If the user returns a response that is unsatisfactory, i.e. is falsy (name === '', name === null, etc) or equivalent to the prompt we gave them, we pop a new prompt with an error message but the same default message.

function getName(ques, def) {
  def = def || ques; // Make default param optional
  var name = prompt(ques, def);

  if (name && name !== '' && name != def) {
    return name;
  } else if(name !== false) {
    return getName("Oops, looks like you didn't enter anything", def);
  }
};

var name = getName("Enter your name");
alert('Your name is: ' + name);

Demo: http://cdpn.io/rlJGj

Be careful with this from a user experience perspective. Its downright horribly for many reasons. If the user hits cancel, this will prompt them until they enter an acceptable value and press ok. It doesn't make for the best user experience, a quite bad one, in fact.

Community
  • 1
  • 1
finiteloop
  • 4,444
  • 8
  • 41
  • 64
0

If you look at the definition of JavaScript's prompt() method, you could see that the second parameter is the default text that the method should return in case the user has not entered anything.

Try this instead:

var name = prompt("Enter your name");

if (name === "") {
    prompt("Oops, looks like you didn't enter anything");
} else if(name != null) {
    alert("Welcome" + " " + name);
}

Hope this helps! Live demo here.

Zorayr
  • 23,770
  • 8
  • 136
  • 129
  • 1
    This is actually the answer that makes the most sense. No need to make the default value "Enter your name" if the prompt is already saying "Enter your name" – richardaday Aug 17 '13 at 04:06
0
<script>
var name=prompt("Enter your name", "Enter your name")
if (name!=='' && name!=='Enter your name') {
alert("Welcome" + " " + name)
} else if(name!==false) {
prompt("Oops, looks like you didnt enter anything");
}
</script>

Demo

animuson
  • 53,861
  • 28
  • 137
  • 147
Mohammad Masoudian
  • 3,483
  • 7
  • 27
  • 45
0

else if(name !== false) will always be true.

since name will never hold the value true; === doesn't do type conversion.

So a little clean up:

var pHolder = "Enter your name"
var name=prompt("Enter your name", pHolder )
if(!name ||name == pHolder){
   alert("Oops, looks like you didnt enter anything");
}
else{
    alert("Welcome" + " " + name)
}
raam86
  • 6,785
  • 2
  • 31
  • 46