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;
}