Forum Discussion

sean_ng's avatar
sean_ng
Occasional Contributor
6 years ago
Solved

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

  • sean_ng's avatar
    sean_ng
    6 years ago

    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

  • shankar_r's avatar
    shankar_r
    Community Hero

    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 + ">");
          }
    }
    • sean_ng's avatar
      sean_ng
      Occasional Contributor

      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 ..

    • sean_ng's avatar
      sean_ng
      Occasional Contributor

      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