Page 1 of 1

Custom isValidNumber in Javascript

Posted: Thu Sep 03, 2009 10:54 am
by ~
I made a custom function in Javascript to verify if a number is a valid binary, octal or hexadecimal. So far I have tested it successfuly with the following numbers: 4.3.3 .43 4.3 0xh 0xH 0 0x00h 00h 00H 3o 38 x333333333 x333333333h 00000b 10101010b 10101010B 101031010B 8o 7o 7oh 7ho 1bo 1ob.

Can you think of other values to see if there are no more hidden bugs, or should it be enough to say everything is correct?

Code: Select all

     alert("4.3.3 "+isValidNumber("4.3.3"));
     alert(".43 "+isValidNumber(".43"));
     alert("4.3 "+isValidNumber("4.3"));
     alert("0xh "+isValidNumber("0xh"));
     alert("0xH "+isValidNumber("0xH"));
     alert("0 "+isValidNumber("0"));
     alert("0x00h "+isValidNumber("0x00h"));
     alert("00h "+isValidNumber("00h"));
     alert("00H "+isValidNumber("00H"));
     alert("3o "+isValidNumber("3o"));
     alert("38 "+isValidNumber("38"));
     alert("x333333333 "+isValidNumber("x333333333"));
     alert("x333333333h "+isValidNumber("x333333333h"));
     alert("00000b "+isValidNumber("00000b"));
     alert("10101010b "+isValidNumber("10101010b"));
     alert("10101010B "+isValidNumber("10101010B"));
     alert("101031010B "+isValidNumber("101031010B"));
     alert("8o "+isValidNumber("8o"));
     alert("7o "+isValidNumber("7o"));
     alert("7oh "+isValidNumber("7oh"));
     alert("7ho "+isValidNumber("7ho"));
     alert("1bo "+isValidNumber("1bo"));
     alert("1ob "+isValidNumber("1ob"));



This is the source code of the function I made:

Code: Select all

function isValidNumber(number_str)
{
 binnum=false;
 octnum=false;
 corrnum=false;
 hexnum=false;
 hextype=0;
 xst=0;
 xsforlength=number_str.length-1;
 pointcount=0;


 if(isNumChar(number_str.charAt(0)))corrnum=true;

 if(number_str.indexOf("0x")==0 &&(
    number_str.indexOf("h")<number_str.length-1 ||
    number_str.indexOf("H")<number_str.length-1
   )){corrnum=true;hexnum=true;hextype=1;xst=2;}


 if(number_str.indexOf("0x")<0 &&(
    number_str.indexOf("h")==number_str.length-1 ||
    number_str.indexOf("H")==number_str.length-1
   )){corrnum=true;hexnum=true;hextype=2;xsforlength--;}


 if(number_str.indexOf("0x")<0 &&(
    number_str.indexOf("o")==number_str.length-1 ||
    number_str.indexOf("O")==number_str.length-1
   )&&number_str.length>1){corrnum=true;octnum=true;xsforlength--;}


 if(number_str.indexOf("0x")<0 &&(
    number_str.indexOf("b")==number_str.length-1 ||
    number_str.indexOf("B")==number_str.length-1
   )&&number_str.length>1){corrnum=true;binnum=true;xsforlength--;}



 if(number_str.length<=2&&corrnum&&hextype==1)corrnum=false;

 if(number_str.length<=1&&corrnum&&hextype==2)corrnum=false;


 if(corrnum)
 {
   for(xst=0;xst<=xsforlength;xst++)
   {
    if(hextype>1&&(
       number_str.charAt(xst)=="a" ||
       number_str.charAt(xst)=="b" ||
       number_str.charAt(xst)=="c" ||
       number_str.charAt(xst)=="d" ||
       number_str.charAt(xst)=="e" ||
       number_str.charAt(xst)=="f" ||

       number_str.charAt(xst)=="A" ||
       number_str.charAt(xst)=="B" ||
       number_str.charAt(xst)=="C" ||
       number_str.charAt(xst)=="D" ||
       number_str.charAt(xst)=="E" ||
       number_str.charAt(xst)=="F"
      )){corrnum=true;hexnum=true;}
    else if(isNumChar(number_str.charAt(xst))){corrnum=true;}
    else if(number_str.charAt(xst)=="."&&
            !binnum&&
            !octnum&&
            !hexnum&&
            pointcount==0){pointcount++;corrnum=true;}


    else{corrnum=false;break;}

    if(binnum&&!(number_str.charAt(xst)>="0"&&number_str.charAt(xst)<="1")){corrnum=false;break;}

    if(octnum&&!(number_str.charAt(xst)>="0"&&number_str.charAt(xst)<="7")){corrnum=false;break;}
   }
 }


 return corrnum;
}




function isNumChar(chr)
{
    if(((chr>='0'&&chr<='9')))
    {return true}else return false;
}

Re: Custom isValidNumber in Javascript

Posted: Thu Sep 03, 2009 7:50 pm
by stephenj
If you can't find a pre-built function, why not just use regular expressions?

Saying "/[01]+b/i" or "/[0-7]+o/i", for example, is pretty simple to read.

One point of caution though. Under C convention, if a number starts with a 0 it is probably octal, unless followed by an x (0x) in which case, hex. And do you want to handle negative numbers?

Re: Custom isValidNumber in Javascript

Posted: Thu Sep 03, 2009 11:51 pm
by ~
stephenj wrote:If you can't find a pre-built function, why not just use regular expressions?

Saying "/[01]+b/i" or "/[0-7]+o/i", for example, is pretty simple to read.

I think I will be porting that snippet to languages like C and below later so I think I need to have the full source code definition of the logic sooner or later.

stephenj wrote:One point of caution though. Under C convention, if a number starts with a 0 it is probably octal, unless followed by an x (0x) in which case, hex. And do you want to handle negative numbers?
I didn't know about that of octal numbers, so I handle decimals normally like 3646746 or 3.346536, octals like 01234567o or 01234567O and hexadecimals like 0123456789ABCDEFh, 0ECFH, 0ECFh or 0xFeDcBa9876543210.

The numbers in the previous post are recognized correctly as valid or invalid correctly for each so far.

I want to handle negative numbers and other notations like 0+3E or something like that, but I think it would be part of the stage that has to do with interpreting the math operators with the precedence. But of course I could be wrong on that.

Probably it wouldn't be good to include negative handling into the most basic function but in a later routine that integrates other ones to make things more "modular".