0

I have added confirmation dialog to input again the status I get before submitting.

That park works. Problem is that prompt() function does not except inputing just numbers?

Is there anyway I can added that part to so it can pass?

$(".delete-status").click(function (ev, el) {

    var status = $(this).data("status");
    var statusInput = prompt("Confirm deletion by entering the status:");

    if (statusInput === status) {

        statusDelete(status);
    } else if (statusInput === null || statusInput === "") {

        alert("Field should not be empty!");
    } else {

        alert("Entered status and status don't match!");
    }
});

Any idea how to fix the code? I am pretty new at jQuery and JS. Thanks

I want to cover both cases. With string and with number.

2
  • 1
    prompt() returns a string. status is an integer. Since you use strict equality, they don't match. Commented Apr 1, 2021 at 8:41
  • You put a check against what the user has entered and loop around until they enter valid data (need some way to exit though). Or, use a different component such as a modal dialog or 3rd-party "prompt" component. prompt() will always be text input without validation, it's only meant as a basic UI component for simple sites / proof-of-concept. Commented Apr 1, 2021 at 8:42

2 Answers 2

1

jQuery's .data() method automatically parses the data as JSON if it can, so if it looks numeric it will return a number, not a string.

prompt() always returns a string (or null if you cancel).

So you need to convert status and statusInput to the same type if you want to compare them with ===. You can use parseInt() to convert the user input to an integer.

$(".delete-status").click(function(ev, el) {

  var status = $(this).data("status");
  var statusInput = parseInt(prompt("Confirm deletion by entering the status:"));

  if (statusInput === status) {

    statusDelete(status);
  } else if (statusInput === null || statusInput === "") {

    alert("Field should not be empty!");
  } else {

    alert("Entered status and status don't match!");
  }
});

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for replay. If I use parseInt in all cases will it affect if the status is just string? @Barmar
You should only use it if you expect the user to enter a number.
The alternative is to convert status to a string: $(this).data("status").toString()
If $(this).data("status") is a string, then parseInt() on the prompt will not match
Other alternatives: if (statusInput == status) (though generally people recommend === as == can cause errors if you don't know what you're doing) (despite the evidence that "===" seems to cause more errors...)
|
0

You could convert statusInput string to a number.

This could be better but something along the lines of:

$(".delete-status").click(function (ev, el) {
    var status = $(this).data("status");
    var statusInput = prompt("Confirm deletion by entering the status:");

    if (Number(statusInput) === Number(status)) {
        statusDelete(status);
    } else if (statusInput === null || statusInput === "") {
        alert("Field should not be empty!");
    } else {
        alert("Entered status and status don't match!");
    }
})

1 Comment

Yes but I think it will be problem as I want to cover both cases. With string and with number.. @MikeW

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.