Allow only Numbers and Decimals in a text box


To allow only Numbers and Decimals in Text boxes you can use the following JavaScript code,

<SCRIPT language=Javascript>
       <!--
       function isNumberKey(evt)
       {
          var charCode = (evt.which) ? evt.which : event.keyCode;
          if (charCode != 46 && charCode > 31 
            && (charCode < 48 || charCode > 57))
             return false;

          return true;
       }
       //-->
    </SCRIPT>

And at the Text box call the function on Key Press event,

onkeypress="return isNumberKey(event)"
Advertisements

One thought on “Allow only Numbers and Decimals in a text box

  1. dilan

    hi..
    when we enter the decimal places will occur this kind of error..

    ReferenceError: event is not defined
    var charCode = (evt.which) ? evt.which : event.keyCode;

    Reply

Let me know what you think .. :)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s