JavaScript – Convert String to Integer

If you are trying to validate numerical calculations on a form, just using would not be useful because JavaScript by default treats the data as string. Therefore, values must be converted to integer values for accurate validations. Here’s how you can convert string to integer in JavaScript. The argument ’10’ refers to the base of 10, by default I believe is Hexadecimal which would again screw up the calculations.

$numServers = $( '#numServers' );
var intval = parseInt(0 + $numServers.val(), '10');

One thought on “JavaScript – Convert String to Integer

  1. Thanks for your marvelous posting! I quite enjoyed reading it,
    you will be a great author. I will make sure to bookmark your blog and
    will often come back in the foreseeable future.
    I want to encourage you to continue your great writing, have a nice afternoon!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s