title: Allow only numbers and dot in script
body: <p>Am using this javascript for restrict users to type only numbers and only one dot as decimal separator.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
   function fun_AllowOnlyAmountAndDot(txt)
          {
              if(event.keyCode &gt; 47 &amp;&amp; event.keyCode &lt; 58 || event.keyCode == 46)
              {
                 var txtbx=document.getElementById(txt);
                 var amount = document.getElementById(txt).value;
                 var present=0;
                 var count=0;
  
                 if(amount.indexOf(".",present)||amount.indexOf(".",present+1));
                 {
                // alert('0');
                 }
  
                /*if(amount.length==2)
                {
                  if(event.keyCode != 46)
                  return false;
                }*/
                 do
                 {
                 present=amount.indexOf(".",present);
                 if(present!=-1)
                  {
                   count++;
                   present++;
                   }
                 }
                 while(present!=-1);
                 if(present==-1 &amp;&amp; amount.length==0 &amp;&amp; event.keyCode == 46)
                 {
                      event.keyCode=0;
                      //alert("Wrong position of decimal point not  allowed !!");
                      return false;
                 }
  
                 if(count&gt;=1 &amp;&amp; event.keyCode == 46)
                 {
  
                      event.keyCode=0;
                      //alert("Only one decimal point is allowed !!");
                      return false;
                 }
                 if(count==1)
                 {
                  var lastdigits=amount.substring(amount.indexOf(".")+1,amount.length);
                  if(lastdigits.length&gt;=2)
                              {
                                //alert("Two decimal places only allowed");
                                event.keyCode=0;
                                return false;
                                }
                 }
                      return true;
              }
              else
              {
                      event.keyCode=0;
                      //alert("Only Numbers with dot allowed !!");
                      return false;
              }
  
          }
  
      &lt;/script&gt;
  
  &lt;td align="right"&gt;
  &lt;asp:TextBox ID="txtQ1gTarget" runat="server" Width="30px" CssClass="txtbx" MaxLength="6" onkeypress="return fun_AllowOnlyAmountAndDot(this);"&gt;&lt;/asp:TextBox&gt;
  &lt;/td&gt;
  </code></pre>
  
  <p>But the onkeypress(this) event returns object required error in that function at this place</p>
  
  <pre><code>var amount = document.getElementById(txt).value;
  </code></pre>
  
  <p>What's my mistake here? </p>