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

Hello

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

open_company('RelMov.asp')



I need to test if my str has only number

thanks
LVL 1
hidrauAsked:
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.

leakim971PluritechnicianCommented:
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

0

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;
}
0
hidrauAuthor Commented:
thanks
0
leakim971PluritechnicianCommented:
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 : http://www.w3schools.com/jsref/jsref_isNaN.asp
But you want only Integer, it allow float for example.
0
hidrauAuthor Commented:
thanks friend for advice

have a nice day
0
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
JScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.