how can I test if a string has a int value in javascript?


I have a value that I will pass through my function, I need to see if the value of my string has a number or only caracter, see the exemple:

function open_company(str){


open_company('201103') => only int value

but sometimes I can have


I need to test if my str has only number

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You may use :
function open_company(str){
   if( !/\D/.test(str) ) {
      // ok we've only number in the string
   else {
      // other char is present

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hidrauAuthor Commented:
hi leakim971,

I found this too, what do you think?

function isNumeric(sText)
   // caso queira utilizar a virgula como separador decimal coloque nesta variável
   var ValidChars = "0123456789.,";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++)
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         IsNumber = false;
   return IsNumber;
hidrauAuthor Commented:
Hi hidrau,

You're welcome! Thanks for the points!

>I found this too, what do you think?
It use a loop, not very good.

Check this one too :
But you want only Integer, it allow float for example.
hidrauAuthor Commented:
thanks friend for advice

have a nice day
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.