Detect empty value on prompt

Toniq

How to detect an empty value when user presses ok on prompt (and previously empties the prompt field)? I need to overwrite the old value with the new (empty) value. I'm doing this:

var oldVal = 'something';
var newVal = prompt("Enter new value:", oldVal);

Currently it returns null if user empties value and clicks ok.

But at the same time I need to check for null value because if user clicks 'cancel', it will return null, which I don't want as new value.

jfriend00

It does not return null if the user hits OK - it will return an empty string. You are probably not testing the return value properly. If you want to test between the three different return states, you can do that like this:

var oldVal = 'something';
var newVal = prompt("Enter new value:", oldVal);
if (newVal === "") {
    // user pressed OK, but the input field was empty
} else if (newVal) {
    // user typed something and hit OK
} else {
    // user hit cancel
}

Working demo: http://jsfiddle.net/jfriend00/Kx2EK/


Your comment suggests that you're using this code to test the result:

if(!newVal || oldVal == newVal)return false;

When the user clears the field and presses OK, newVal will be "" (an empty string). !newVal will be true so you will return false. An empty string is a falsey value just like null. You need to more explicitly check for null like this:

if (newVal === null || newVal === oldVal) {
    // cancel button was hit
    // or the same value was entered
    return false;
}

Working demo of this logic: http://jsfiddle.net/jfriend00/ynwBx/

Note: I'm using === to prevent the javascript interpreter from doing any type casting as I want to only explicitly check for null.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Using a variable as a default value in vars_prompt in Ansible

来自分类Dev

How to display an empty value in NumericUpDown control?

来自分类Dev

Resetting a Bacon property on value changed to empty

来自分类Dev

How to empty a numeric value from a form built with WTForm

来自分类Dev

Symfony2选择字段必须为empty_value

来自分类Dev

Symfony2选择字段必须为empty_value

来自分类Dev

未找到 SelectSingleNode.value 时分配 string.empty

来自分类Dev

在Symfony 2.4中的formBuilder中将empty_value选项设置为“禁用”

来自分类Dev

How can I find out if an data- attribute is set to an empty value?

来自分类Dev

Db2 JSON_VALUE ERROR ON EMPTY无法正常工作

来自分类Dev

使用 ??字符串null或empty和DBNull.Value的运算符

来自分类Dev

此模式是否有名称“type 'a foldedSequence = Empty | Value of 'a * (unit->'a foldedSequence )”

来自分类Dev

如何在 PHP 中获取一个 empty_value 的值

来自分类Dev

Powershell - Prompt user then expire prompt after xx seconds

来自分类Dev

角色的Ansible vars_prompt

来自分类Dev

zsh使用多行声明PROMPT

来自分类Dev

如何修改PROMPT以包含变量?

来自分类Dev

如何使bootbox.prompt工作?

来自分类Dev

Why is my empty array not empty?

来自分类Dev

Detect if image is embedded

来自分类Dev

Javascript Proxy: detect recursions

来自分类Dev

Detect what raises an exception

来自分类Dev

了解`detect`方法

来自分类Dev

这个PROMPT_COMMAND有什么作用?

来自分类Dev

Docker bash prompt does not display color output

来自分类Dev

"Could not find or load main class" command prompt

来自分类Dev

How to get a prompt for information using a batch file?

来自分类Dev

Ansible:在vars_prompt中输出变量

来自分类Dev

Windows Login Prompt When Accessing Site

Related 相关文章

热门标签

归档