View Single Post
 
Old February 11th, 2008, 04:51 PM
vinod_yadav1919 vinod_yadav1919 is offline
Friend of Wrox
 
Join Date: Oct 2004
Location: delhi, delhi, India.
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hi Everest!!
Hope this will help you :)
<script>
function storeCaret (x) {
 if (!document.all) {return}
  textEl = x
  if (textEl.createTextRange)
     caretPos = document.selection.createRange();

 }

function update (field,event,precision22) {

var isIE = document.all?true:false;
var isNS = document.layers?true:false;
var key = (isIE) ? window.event.keyCode : e.which;
var obj = (isIE) ? event.srcElement : e.target;

if ( (event.keyCode >47&& event.keyCode <58)||event.keyCode ==46)
{
  var icaretPos = (caretPos) ? caretPos : textEl.length;
  icaretPos.text = "|";
   theValue = textEl.value;
  temp_original=theValue;
  var arr=theValue.split("|");
   toTheLeft=arr[0].split(",")[arr[0].split(",").length-1];
  toTheRight = arr[1].split(',')[0];
    if(toTheLeft.indexOf(".")>=0)
        {
         dec_right=temp_original.indexOf(".")
         dec_left=temp_original.substring(dec_right+1,temp_ original.length)
         dec_left_length=dec_left.length
                    if(event.keyCode!=46&&dec_left_length<((precision2 2)+1))
                            { event.returnValue = true;}
                       else
                           { event.returnValue = false;}
       }

        if((toTheRight.indexOf(".")>=0)&&event.keyCode ==46)
                     {event.returnValue = false;}
     caretPos.moveStart("character", -1);
     caretPos.text="";
  }
else
  event.returnValue = false;

 }


 </script>

</head>

<body >
<form>
 <table><tr>
<td>only2Digits after decimal<input name="txt" onkeypress="storeCaret(this);update(this,event,2)" ></td>
</tr>
<tr><td>only5Digits after decimal<input name="txt2" onkeypress="storeCaret(this);update(this,event,5)" ></td>
 </tr>
 </table>
</form>
<br>
</body>
</html>

_____________************_______________
It will only allow decimal(./dot) ,no other characters.
You can specfiy number of decimal digits
You can enter other number after decimal
You can't put more than one dot(.)

Please a look on the following link too
http://www.experts-exchange.com/Prog..._20739343.html


Cheers :)

vinod