davej Posted December 26, 2012 Share Posted December 26, 2012 (edited) I'm having a problem in Chrome with some navigation values causing exceptions to be thrown when .toFixed is used on them. I am now trying to use... temp = position.coords....if (temp != null && Number(temp) != NaN)data += temp.toFixed(8); But this isn't working 100% either, at least not in Firefox. What is the best way to screen for invalid numbers? Edited December 26, 2012 by davej Link to comment Share on other sites More sharing options...
Ingolme Posted December 26, 2012 Share Posted December 26, 2012 You can use a regular expression to check that only numbers are used. if(!/[0-9]+(\.?[0-9])?/.test(str)) { alert("Number not valid.");} Link to comment Share on other sites More sharing options...
davej Posted December 27, 2012 Author Share Posted December 27, 2012 (edited) I thought that was the whole point of the Number() function? Oh, I think I solved it using isNaN() http://www.w3schools.com/jsref/jsref_isnan.asp Edited December 27, 2012 by davej Link to comment Share on other sites More sharing options...
Ingolme Posted December 28, 2012 Share Posted December 28, 2012 The point of Number() isn't to validate numbers, but to cast to a number.When casting to number, as long as the first character is numeric it is considered a number and it will continue to parse until it finds a non.numeric character. Now that I think of it, NaN is never equal to another NaN. The only way to check that a "number" is not a number is to use isNaN(). Link to comment Share on other sites More sharing options...
davej Posted December 29, 2012 Author Share Posted December 29, 2012 So really the best test would be? if ( isNaN( Number(x) ) ) {alert('NaN');} Link to comment Share on other sites More sharing options...
Ingolme Posted December 29, 2012 Share Posted December 29, 2012 That should work. However, if x is "5abc" it will evaluate to 5 and be a number. If you're fine with that then your script is OK. Link to comment Share on other sites More sharing options...
davej Posted December 29, 2012 Author Share Posted December 29, 2012 Yes, I've run into that before. Is there a scheme that would reject "5abc" ? Link to comment Share on other sites More sharing options...
Ingolme Posted December 29, 2012 Share Posted December 29, 2012 A regular expression would, which is what I provided earlier. Link to comment Share on other sites More sharing options...
boen_robot Posted January 1, 2013 Share Posted January 1, 2013 Why would you reject that if you can just use the number value though?I mean, you can simply do: x = new Number(x);if (!isNaN(x)) { //No problem. Continue with "x", that is now surely a valid number.} Link to comment Share on other sites More sharing options...
Ingolme Posted January 3, 2013 Share Posted January 3, 2013 Perhaps it's validation before passing to the server-side. PHP casts to numbers using the same rules as Javascript using (int) or intval(), (float) or floatval() Link to comment Share on other sites More sharing options...
davej Posted January 3, 2013 Author Share Posted January 3, 2013 If a user entered 5abc you might want to reject it simply because a number was expected and they entered something goofy. I had not thought of using a regex for that purpose. Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now