Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect empty value on prompt

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.

like image 300
Toniq Avatar asked Dec 12 '13 01:12

Toniq


People also ask

How do you check if a value is empty?

The IS NULL operator is used to test for empty values (NULL values).

What does an empty prompt return?

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.

How do you use prompt?

A prompt box is used if you want the user to input a value. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed. Do not overuse this method. It prevents the user from accessing other parts of the page until the box is closed.

How to check if value is empty in JavaScript?

Check if value is empty in JavaScript Javascript Web Development Object Oriented Programming Use the condition with “” and NULL to check if value is empty. Throw a message whenever ua ser does not fill the text box value.

How to check if a text is entered or empty?

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. Show activity on this post.

How do you know if an input is empty?

We want to know whether an input is empty. For our purpose, empty means: The user hasn’t typed anything into the field The user has typed one or more empty spaces, but not other characters

How do you check if a list is empty in Python?

What is best way to check if a list is empty in Python? Use the condition with “” and NULL to check if value is empty. Throw a message whenever ua ser does not fill the text box value.


Video Answer


1 Answers

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.

like image 173
jfriend00 Avatar answered Oct 07 '22 22:10

jfriend00