raghunathmitte Posted December 6, 2008 Share Posted December 6, 2008 try{String quantity=parseInt("abc123");if(quantity == NaN)throw "quantity cannot be null";}catch(exception){ alert(exception);}i have the above code in a function when i try to convert the string into int parseInt() method returns NaN value but if condition is not executing, is there any other way to compare with NaN?? please help me out Link to comment Share on other sites More sharing options...
jeffman Posted December 6, 2008 Share Posted December 6, 2008 Use isNaN(number) Link to comment Share on other sites More sharing options...
Synook Posted December 7, 2008 Share Posted December 7, 2008 You don't even need to use try ... catch (which is memory-intensive): var quantity = "abc123";if (!isNaN(quantity)) quantity = parseInt(quantity);else alert("Quantity is Not a Number!"); Link to comment Share on other sites More sharing options...
rnd me Posted December 8, 2008 Share Posted December 8, 2008 parseInt cannot handle letter in front of the number, only behind the number. Link to comment Share on other sites More sharing options...
inktherapy Posted December 9, 2008 Share Posted December 9, 2008 parseInt can only return numerical figures. Link to comment Share on other sites More sharing options...
jesh Posted December 9, 2008 Share Posted December 9, 2008 parseInt cannot handle letter in front of the number, only behind the number.Sure it can - as long as you are using a base that is greater than 10:var str = "rundmc";var num = parseInt(str, 36);alert(num); Link to comment Share on other sites More sharing options...
rnd me Posted December 9, 2008 Share Posted December 9, 2008 Sure it can - as long as you are using a base that is greater than 10:var str = "rundmc"; var num = parseInt(str, 36); alert(num); somehow i doubt that's what the OP had in mind...and thinking a little more about it, you are wrong.in your example, "rundmc" is a number, so there once again are no letters in front of the number... Link to comment Share on other sites More sharing options...
Synook Posted December 10, 2008 Share Posted December 10, 2008 Depends how you define "letter" and "number" Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.