Specializations

Wednesday, November 14, 2012

IsNumber in javascript method

Is NUmber

ASP.NET 
  <asp:TextBox ID="txtWeight" Width="135px" runat="server" CssClass="textbox" onkeypress="return ValidateNumeric(this,event);"></asp:TextBox>
 JAVASCRIPT
function ValidateNumeric(elementRef,event)
  {
     var keyCodeEntered = (event.which) ? event.which : (window.event.keyCode) ? window.event.keyCode : -1;

     if ( (keyCodeEntered >= 48) && (keyCodeEntered <= 57) || (keyCodeEntered ==8) || (keyCodeEntered == 127 ) )
     {
     return true;
     }
     else if ( keyCodeEntered == 46 )
     {
     // Allow only 1 decimal point ('.')...
     if ( (elementRef.value) && (elementRef.value.indexOf('.') >= 0) )
     {
      return false;
      }
     else
     return true;
      }
   return false;
  }        

No comments:

Post a Comment