How to display words for numbers types in a text box?
This can be done using by javascript which makes user delighted for updating the text for every number press..
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function dispWrds(txtID,lblID)
{
var txt=document.getElementById(txtID);
var lbl=document.getElementById(lblID);
var val=txt.value;
var actVal=txt.value;
while(val.indexOf("0")==0)
{
val=val.substring(1);
}
actVal=val;
var txt='';
if(val.length==0)
{
txt="0";
}
else
{
val=actVal;
var prevVal;
var vals= getAmtArrays(actVal);
for(var k=vals.length-1;k>=0;k--)
{
val=vals[k];
if(k!=vals.length-1)
{
if(prevVal.length==1)
txt+=' Crore ';
else
txt+=' Crores ';
}
if(val.length==1)
{
txt+=GetWrd1(val);
}
if(val.length==2)
{
txt+=get2Wrd(val);
}
if(val.length==3)
{
txt+=get3Wrd(val);
}
if(val.length==4)
{
txt+=get4Wrd(val);
}
if(val.length==5)
{
txt+=get5Wrd(val);
}
if(val.length==6)
{
txt+=get6Wrd(val);
}
if(val.length==7)
{
txt+=get7Wrd(val);
}
prevVal=val;
}
}
lbl.innerText=txt;
}
function getAmtArrays(val)
{
var rt=new Array();
var ind=-1;
var lp=0;
for(var i=val.length-1;i>=0;i--)
{
if(lp%7==0 || i==val.length-1)
{
ind++;
rt[ind]=val.charAt(i);
}
else
{
rt[ind]+=val.charAt(i);
}
lp++;
}
for(var j=0;j<=rt.length-1;j++)
{
var arr;
for(var i=rt[j].length-1;i>=0;i--)
{
if(i==rt[j].length-1)
arr=rt[j].charAt(i);
else
arr+=rt[j].charAt(i);
}
rt[j]=arr;
}
return rt;
}
function get7Wrd(vl)
{
var val1=vl.charAt(0)+vl.charAt(1);
var val2=vl.charAt(2)+vl.charAt(3);
var val3=vl.charAt(4);
var val4=vl.charAt(5)+vl.charAt(6);
var vl1=get2Wrd(val2);
var vl2=GetWrd1(val3);
return (get2Wrd(val1)==''?'':get2Wrd(val1)+' lakhs ')+
(vl1==''?'': vl1+' Thousand ')+
(vl2==''?'':vl2+' Hundred ')+
get2Wrd(val4);
}
function get6Wrd(vl)
{
var val1=vl.charAt(0);
var val2=vl.charAt(1)+vl.charAt(2);
var val3=vl.charAt(3);
var val4=vl.charAt(4)+vl.charAt(5);
var vl1=get2Wrd(val2);
var vl2=GetWrd1(val3);
return (GetWrd1(val1)==''?'':GetWrd1(val1)+' lakh ')+
(vl1==''?'': vl1+' Thousand ')+
(vl2==''?'':vl2+' Hundred ')+
get2Wrd(val4);
}
function get5Wrd(vl)
{
var val1=vl.charAt(0)+vl.charAt(1);
var val2=vl.charAt(2);
var val3=vl.charAt(3)+vl.charAt(4);
var vl1=GetWrd1(val2);
return (get2Wrd(val1)==''?'':get2Wrd(val1)+' Thousand ')+
(vl1==''?'':vl1+' Hundred ')+get2Wrd(val3);
}
function get4Wrd(vl)
{
var val1=vl.substring(0,1);
var val2=vl.charAt(1);
var val3=vl.substring(2);
var vl1=GetWrd1(val2);
return (GetWrd1(val1)==''?'':GetWrd1(val1)+' Thousand ')
+(vl1==''?'':vl1+' Hundred ')+get2Wrd(val3);
}
function get3Wrd(vl)
{
var val1=vl.substring(0,1);
var val2=vl.substring(1);
return (GetWrd1(val1)==''?'':GetWrd1(val1))+' Hundred '+get2Wrd(val2);
}
function get2Wrd(vl)
{
var val1=vl.substring(0,1);
var val2=vl.substring(1);
if(val1=='1' && val2!=0)
{
return GetWrd11(vl);
}
else if(val2!=0)
{
return GetWrd2(val1+'0')+' '+GetWrd1(val2);
}
else
{
return GetWrd2(vl);
}
}
function GetWrd1(no)
{
if(no=='1')
{
return 'One';
}
if(no=='2')
{
return 'Two';
}
if(no=='3')
{
return 'Three';
}
if(no=='4')
{
return 'Four';
}
if(no=='5')
{
return 'Five';
}
if(no=='6')
{
return 'Six';
}
if(no=='7')
{
return 'Seven';
}
if(no=='8')
{
return 'Eight';
}
if(no=='9')
{
return 'Nine';
}
if(no=='0')
{
return '';
}
}
function GetWrd11(no)
{
if(no=='11')
{
return 'Eleven';
}
if(no=='12')
{
return 'Twelve';
}
if(no=='13')
{
return 'Thirteen';
}
if(no=='14')
{
return 'Fourteen';
}
if(no=='15')
{
return 'Fifteen';
}
if(no=='16')
{
return 'Sixteen';
}
if(no=='17')
{
return 'Seventeen';
}
if(no=='18')
{
return 'Eighteen';
}
if(no=='19')
{
return 'Nineteen';
}
}
function GetWrd2(no)
{
if(no=='11')
{
return 'Eleven';
}
if(no=='10')
{
return 'Ten';
}
if(no=='20')
{
return 'Twenty';
}
if(no=='30')
{
return 'Thirty';
}
if(no=='40')
{
return 'Fourty';
}
if(no=='50')
{
return 'Fifty';
}
if(no=='60')
{
return 'Sixty';
}
if(no=='70')
{
return 'Seventy';
}
if(no=='80')
{
return 'Eighty';
}
if(no=='90')
{
return 'Ninety';
}
if(no=='00')
{
return '';
}
}
function chk_Number()
{
return true;
return false;
}
function IsNumber(val)
{
var nums="0123456789";
if(nums.indexOf(val)==-1)
return false;
return true;
}
function textPasted(obj)
{
var str=obj.value;
var finVal='';
for(var i=0;i<obj.value.length;i++)
{
if(IsNumber(str.charAt(i)))
{
finVal+=str.charAt(i);
}
}
obj.value=finVal;
}
</script>
</head>
<body>
<br />
<input " onkeyup="dispWrds('txtAmount','lblAmount');" onkeypress="return chk_Number();" onpaste="return false;" onmouseover="textPasted(this);"/>
<br />
<label id="lblAmount" ></label>
</body>
</html>
You can observe for every key press made by the user updates the relevant number in words and displays in the bottom label.
No comments:
Post a Comment