简体   繁体   English

如何创建显示基于提示输入类型的文本的警报弹出窗口?

[英]How to create Alert pop up that shows text based on Prompt input type?

I am a beginner experimenting with some basic Javascript. 我是尝试一些基本Javascript的初学者。 My goal is to show a (1.) prompt message (requesting a name) followed by an (2.) alert based on the prompt input. 我的目标是显示(1.)提示消息(请求名称),然后根据提示输入显示(2.)警报。 If the prompt input is a valid name (string) then an alert box should appear with text "thank you" + name. 如果提示输入的是有效名称(字符串),则将出现一个警告框,其中包含文本“谢谢” +名称。 Else the alert should appear with the text "you didn't enter a valid name". 否则,警报应显示为文本“您未输入有效名称”。

The 1. prompt message is working, however, the 2. alert message shows me the same text whether i enter a name/text or a number. 1.提示消息有效,但是,无论输入名称/文本还是数字,2.警报消息都显示相同的文本。 In other words the alert message is not distinguishing between text string or number and is always showing the text "thank you" + name. 换句话说,警报消息不会区分文本字符串或数字,并且始终显示文本“谢谢” +名称。

this is my code: 这是我的代码:

 function EnterName() { var name = prompt("Enter your name here:"); if (typeof name === "string") { alert("thank you " + name); } else if (typeof name === "number") { alert("you didn't enter a valid name"); } } console.log(EnterName()); 

Would appreciate any insights in how to make the alert box show "you didn't enter a valid name" when a number is entered in the prompt box. 在提示框中输入数字后,如何使警报框显示“您未输入有效名称”的任何见解,我们将不胜感激。

Thanks. 谢谢。

Prompt always returns a string, you could try to convert it to a number and then check to see if it NaN's on you which means it's a string. 提示总是返回一个字符串,您可以尝试将其转换为数字,然后检查它是否是NaN,这意味着它是一个字符串。 It would be better to use a regexp here though. 不过最好在这里使用正则表达式。 You may also want to return the name if you are logging it. 如果您正在登录,则可能还需要返回名称。

function EnterName() {
  var name = prompt("Enter your name here:");
  var isNum = !isNaN(Number(name));
  if (!isNum) {
    alert("thank you " + name);
  } else {
    alert("you didn't enter a valid name");
  }
  return name;
}
console.log(EnterName());

You always enter strings into prompts. 您总是在提示中输入字符串。 SO you can try to make it a int below and if it succeeds then give an error message otherwise say hello. 因此,您可以尝试在下面将其设置为int,如果成功,则给出错误消息,否则打个招呼。

function EnterName() {
  var name = prompt("Enter your name here:");
  if(parseInt(name)) {
    alert("you didn't enter a valid name");
  }
  else if (!parseInt(name)) {
    alert("thank you " + name);
  }
}

prompt method parameters type are alway String , no matter what you enter, your condition will always true : 提示方法的参数类型始终为String ,无论您输入什么,您的条件始终为true:

 //text & defaultText types are String
`prompt(text, defaultText)`

So you need to change your condition. 因此,您需要更改条件。 One solution is using regex for example you can change your code to this : 一种解决方案是使用正则表达式 ,例如,您可以将代码更改为此:

function EnterName() {
   var name = prompt("Enter your name here:");
   !name.match(/^\d+$/) ?
        alert("thank you " + name)
    :     
        alert("you didn't enter a valid name");
}
console.log(EnterName());

Basically !name.match(/^\\d+$/) will check if input data is number or not and based on that will show the alert messages. 基本上, !name.match(/^\\d+$/)将检查输入数据是否为数字,并以此为基础显示警报消息。

https://jsfiddle.net/6mxL59k0/1/ https://jsfiddle.net/6mxL59k0/1/

So, one thing I want to emphasize is how you are approaching solving this problem. 因此,我要强调的一件事是您如何解决这个问题。 You are looking at your problem thinking, "If I type in a number, this should be false! Why isn't it working?!". 您正在思考问题,“如果输入数字,这应该是错误的!为什么它不起作用?!”。 The reason why is because prompt will always return a value that is typed to a string . 之所以这样,是因为提示符将始终返回键入string You are looking to see if there is a number or an int depending on the language. 您正在查看是否有numberint具体取决于语言。

So, we can break this into two problems. 因此,我们可以将其分为两个问题。 See if this guy is a string, see if that string contains a number 查看此人是否为字符串,查看该字符串是否包含数字

The code below shows a great way to do that using two functions 下面的代码显示了使用两个函数来实现此目标的好方法

function enterName() { // Javscript functions should always start with a lowercase unless it is a function express and can be invoked using the `new` keyword
  var name = prompt("Enter your name here:");
  if (!checkIfContainsNumber(name)) {
    alert("thank you " + name);
  } else { // if f we dont have a number, we have a valid name, otherwise we are invalid!
    alert("you didn't enter a valid name");
  }
}

So we have our main function, it takes in the promp which always returns a string 因此,我们有了main函数,它接收总是返回string

// this solves problem 2, seeing if we have a number inside of our string
function checkIfContainsNumber(myString) {
  return /\d/.test(myString);
}

enterName();

our second function checkIfContainsNumber solves part 2 of our problem using a regex expression that matches for a digit ( d in this case) 我们的第二个函数checkIfContainsNumber使用与数字匹配的正则表达式来解决问题的第二部分(本例中为d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM