cancel
Showing results for 
Search instead for 
Did you mean: 

How to check if a string is a number

SOLVED
Occasional Contributor

How to check if a string is a number

[ jscript ] i would like to check if the string i obtain is number or alphabets, something like

 

if numeric(stringABC) then

// do something

 

else

// show error

 

how to write this in jscript ?

thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Occasional Contributor

Re: How to check if a string is a number

hi Shankar_r,

 

thank you for your reply,

i was lost and don't know how to start writing the code but your code help me to kick start writing, i will keep your code for future reference, thanks a lot.

 

This is what i manage to come up with :

 

var ABC = 123;

if(isNaN(ABC)) { Log.Message(ABC + " is not a number"); } else { Log.Message(ABC + " is a number"); }

 

this code work well without any problem for me, i am not sure if this is the correct code for test complete because NaN is what i get from jscript, hopefully it won't cause any bug in future, haha ..

4 REPLIES 4
Community Hero

Re: How to check if a string is a number

A simple solution is,

 

function isNumaric(){
      
      try{
            if(aqConvert.StrToInt(str)){
                  Log.Checkpoint("Coverted Sring to Int <" + str + ">");
            }
      }catch(ex){
            Log.Error("Unable covert Sring to Int <" + str + ">");
      }
}

Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”


Extension Available

Community Hero

Re: How to check if a string is a number

Hi,

 

To add to Shankar's reply:

You provided too little details to get a precise answer. For example, what base (decimal, octal, hexadecimal, ...) must be considered? What if the string starts with figures with some followup characters? Roman numbers? Etc.

Considering that you are using JScript, this might help:

https://www.w3schools.com/jsref/jsref_parseint.asp

https://www.google.com/search?q=jscript+convert+string+to+integer

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Occasional Contributor

Re: How to check if a string is a number

hi Shankar_r,

 

thank you for your reply,

i was lost and don't know how to start writing the code but your code help me to kick start writing, i will keep your code for future reference, thanks a lot.

 

This is what i manage to come up with :

 

var ABC = 123;

if(isNaN(ABC)) { Log.Message(ABC + " is not a number"); } else { Log.Message(ABC + " is a number"); }

 

this code work well without any problem for me, i am not sure if this is the correct code for test complete because NaN is what i get from jscript, hopefully it won't cause any bug in future, haha ..

Occasional Contributor

Re: How to check if a string is a number

hi AlexKaras,

 

thank you very much for your reply, i manage to write the code and i posted a reply here as well, to share my workable code with others.

 

i am new in coding (1 week old, haha), don't even know what question to ask, will take note on the things you mention, will improve my question asking skill on coding in future

 

thanks a lot