Link to home
Start Free TrialLog in
Avatar of hidrau
hidrauFlag for Brazil

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hidrau

ASKER

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;
}
Avatar of hidrau

ASKER

thanks
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.
Avatar of hidrau

ASKER

thanks friend for advice

have a nice day