chob1234 Posted May 21, 2019 Share Posted May 21, 2019 I am needing to convert an String input into an Integer. But before converting the String input, i have to do a validation that the String input can be converted to a numeric value. So If it cannot, i need to log a message saying “This cannot”. Then log another new log statement on the next line saying “be a number”. I understand I need a parseInt and isNan and if else statements but I am confused on exactly how they are implemented. I am new to javascript. Link to comment Share on other sites More sharing options...
Funce Posted May 21, 2019 Share Posted May 21, 2019 You could create a function to check if the number can be converted. function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); } Then all you need to do is if(isNumeric(someNumber)) { //You have a number } else { //You don't have a number } Implement parseInt as required. Use console.log as specified in your post. 1 Link to comment Share on other sites More sharing options...
chob1234 Posted May 22, 2019 Author Share Posted May 22, 2019 (edited) 19 hours ago, Funce said: You could create a function to check if the number can be converted. function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); } Then all you need to do is if(isNumeric(someNumber)) { //You have a number } else { //You don't have a number } Implement parseInt as required. Use console.log as specified in your post. So I came up with this: function isNumeric(string) { return !isNaN(parseInt(string)) && isFinite(string); } if(isNumeric(parseInt <= 10000)) { //You have a number logger.debug("You have a number") } else { //You don't have a number logger.debug("This cannot be a number") } It only outputs "This cannot be a number" What am I doing wrong? I am logging in Thingworx so that is where the logger.debug is coming from. Edited May 22, 2019 by chob1234 Link to comment Share on other sites More sharing options...
justsomeguy Posted May 22, 2019 Share Posted May 22, 2019 if(isNumeric(parseInt <= 10000)) { What exactly are you trying to do there? parseInt is a function, why are you trying to check if a function is less than or equal to 10000? Link to comment Share on other sites More sharing options...
chob1234 Posted May 22, 2019 Author Share Posted May 22, 2019 6 minutes ago, justsomeguy said: if(isNumeric(parseInt <= 10000)) { What exactly are you trying to do there? parseInt is a function, why are you trying to check if a function is less than or equal to 10000? Yeah I am not sure exactly, I guess the goal is to convert the string input to an int, but confused. Link to comment Share on other sites More sharing options...
chob1234 Posted May 22, 2019 Author Share Posted May 22, 2019 58 minutes ago, chob1234 said: Yeah I am not sure exactly, I guess the goal is to convert the string input to an int, but confused. Got it all working now, 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