Detect empty value on prompt
Asked Answered
A

8

15

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.

Attaway answered 12/12, 2013 at 1:5 Comment(1)
prompt does not return NULL when the input is empty and OK is pressed; it returns the empty string.Chare
D
29

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.

Dorkas answered 12/12, 2013 at 1:11 Comment(3)
But I need to have this in my statement: if(!newVal || oldVal == newVal)return false; so I need to check for null. And if the user empties prompt field, then presses ok, null will be returned.Attaway
@Attaway - you don't seem to understand that !newVal will also match an empty string so you aren't distinguishing between the null (cancel button) and an empty string (ok button). I've added code to my answer that will show you how to do what you asked.Dorkas
@DomVinyard - What exactly doesn't work? What do you see on what button and what value of the field in which demo?Dorkas
D
3

A Bit old, but it may be worthwhile to simply include a test for the string's length in the same if() statement

if(newVal !=null & newVal.length>0){

Check both for Cancel and an empty entry.

Deina answered 9/8, 2016 at 13:18 Comment(1)
Have you tested this? There should be &&, not binary & (although I'm not sure if & will work here as well)Leuco
H
2

In most browsers prompt will return null if user presses cancel, but in some (Safari, for ex.) it returns empty string. If user enters some text and presses ok, prompt will return entered text, if user pressed ok without entering text, it will return empty string.

Hemimorphite answered 2/4, 2014 at 15:8 Comment(0)
U
0
<html>
<body>    
<p>Click the button to demonstrate the prompt box.</p>    
<button onclick="myFunction()">Try it</button>    
<p id="demo"></p>    
<script>
function myFunction() {
    var answer=prompt('Reason for deletion?');
    var n = answer.length;
    if(n > 5)
    {
    document.getElementById("demo").innerHTML =
    "Hello " + answer + "! How are you today?";
    }
    else if(n == 0)
    {  
    myFunction()
    } 
}
</script>    
</body>
</html>
Uralic answered 9/3, 2017 at 4:37 Comment(2)
Please provide explanation as wellSurpass
You can check null value in prompt box... if the the prompt box return a null , then it return to the same function...Uralic
V
0

I have see other comment that seem like solved your problem, but I think it might can no t be the best answer or solution here is your code

if (newVal == "") {
//action
}

it might seem work, but it might probably cause a problem or a little bug, is that the user can just type two blank or white-space to bypass your code, here is my solution

If (newVal.trim() == "") {
 //action
}
Vivienne answered 12/12, 2020 at 10:26 Comment(0)
W
0

When user hits OK without entering any value, it returns NaN and you can check it by isNaN() function.

Weidar answered 23/2, 2022 at 7:33 Comment(0)
A
0
// this is TRUE, when user presses Cancel / ESC
window.prompt('Type whatever', '') === null

// this is TRUE, when presses Enter and value is empty
// note that it will be FALSE if canceled or if "0" is entered
window.prompt('Type whatever', '') == ''

Try here: https://jsfiddle.net/L5zrdeq9/

Accustom answered 28/10, 2023 at 18:43 Comment(0)
W
-1

Simple to see if both cancel or ok with empty string

function valueIsSet(value)
{
    return (value && value !== "")
}

// example:

var name;
while (!valueIsSet(name) {
    name = prompt("Please enter your name");
}

alert("Welcome " + name);
World answered 10/7, 2017 at 10:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.