Reputation: 103
I am working on an assignment for school which uses javascript to create functions using forms. I have created the function and does what I need it to do, but to some extent. The function i am creating is to check if a text box follows the constraints i am setting for it using the javascript function. if the user enters a number that is outside of the range, an alert box will show up saying to enter a number between the specified range. I know a lot of people will be using if statements, but i wanted to try a different approach. I am trying with while and do...while statements. I have the function created, but everytime the alert box shows up and i press ok, the alert still keeps showing up. Is there a way from my coding to have it so the alert box does not appear after everytime i click ok? My code:
function checkPrice(formData)
{
while(!(formData.stickerPrice.value > 0) || (formData.stickerPrice.value < 100000))
{
alert("Enter a value between 0 andd 100000");
setTimeout (function()
{
formData.stickerPrice.focus();
})
}
}
Upvotes: 0
Views: 66
Reputation: 50905
Just change it from while
to if
. That way it won't continuously loop. The while
loop's condition will continue to be true
until you break away from the Javascript and let the user type something in. The condition will always be true
once it's entered, since nothing in the condition changes before the next iteration.
Upvotes: 1