$('#save').click(function(){
($('input[type=text]').each(function () {
if (isNaN($(this).val())) {
alert("Some of your values were not entered correctly.");
return 0;
}
});
});
I can’t figure out why this isn’t working. Any suggestions?
Edit: Sorry for the misleading title–I need to make sure the text boxes contain ONLY numbers.
The
isNaNfunction checks for a specific value calledNaN[MDN], which is the result of trying to perform arithmetic operations on non-number objects. It doesn’t check if a string is a number. How about a regular expression?The above will accept positive integers. If you need to accept negative or floating point numbers, change the regular expression to:
Also, if you return 0 from within
each, that stops the loop, but that doesn’t prevent the click event from continuing on. You have invalid data, you need to callevent.preventDefault()to halt the click event. Something like this will do:Working example: http://jsfiddle.net/FishBasketGordo/ER5Vj/