I have a prompt that basically is a required field and cannot contain a decimal. I have a while loop that should continue to prompt the user for information until a number is indicated regardless of whether OK or Cancel are clicked. Everything works fine as long as the OK button is clicked. It continues to prompt if left blank and OK is clicked or if a number with a decimal is provided and OK is clicked. But if cancel is clicked, it doesn’t continue to prompt.
var rmiles = prompt("Please indicate actual miles driven for payroll");
while (rmiles == null | rmiles == "null" | rmiles == " " | rmiles.indexOf(".") != -1) {
alert("Mileage is required when arriving on site and can only be whole numbers. No Decimals. Please enter 0 if you did not intend to arrive on site.");
rmiles = prompt("Please indicate actual miles driven for payroll");
}
I believe the problem is in your
whilecondition – you are doing bitwise OR|on all the conditions. This means thatrmiles.indexOf(".")is always called, even whenrmilesis set tonullbypromptwhen Cancel is clicked. This is because bitwise OR will not short-circuit.Try the logical OR
||which does short-circuit and thus avoids the null reference error: