admin管理员组

文章数量:1426188

I Used this code for restricting to type alphabets and characters.

  <script type="text/javascript">
    function isNumber(evt) {
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            return false;
        }
        return true;
    }

</script>
<asp:TextBox ID="txtCode" runat="server" Width="15%"   onkeypress="return isNumber(event)"/>

But I am able to Paste the alphabets and characters in it(Which I don't want).

When I used this code, The Paste Option is disabled but,I cant Paste even Numbers

   <asp:TextBox ID="txtCode" runat="server" Width="15%" ondrop="return false;"
     onpaste="return false;" onkeypress="return isNumber(event)" />

But I want to paste the Numbers :(,How can I do that ?.

I Used this code for restricting to type alphabets and characters.

  <script type="text/javascript">
    function isNumber(evt) {
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            return false;
        }
        return true;
    }

</script>
<asp:TextBox ID="txtCode" runat="server" Width="15%"   onkeypress="return isNumber(event)"/>

But I am able to Paste the alphabets and characters in it(Which I don't want).

When I used this code, The Paste Option is disabled but,I cant Paste even Numbers

   <asp:TextBox ID="txtCode" runat="server" Width="15%" ondrop="return false;"
     onpaste="return false;" onkeypress="return isNumber(event)" />

But I want to paste the Numbers :(,How can I do that ?.

Share Improve this question edited Jul 27, 2017 at 13:42 Frost_Mourne asked Jul 21, 2017 at 12:02 Frost_MourneFrost_Mourne 3424 silver badges23 bronze badges 2
  • You can opt for third party plugin. Limit Text Box jQuery. When plugin configuration is set to "numbers" will limit characters to only numbers only. Any pasted non-numeric characters will be filtered out and what remains will be a number. – Suprabhat Biswal Commented Jul 21, 2017 at 12:10
  • tried but not working either ?please give me a specific code..that will be great – Frost_Mourne Commented Jul 21, 2017 at 12:21
Add a ment  | 

2 Answers 2

Reset to default 2

First of all you should use type="number" to have a semantic meaning and form validation like HtML5 in mordern browser, and to get the live functionality in all the browser you can try this:

function isNumber(ev) {
    if (ev.type === "paste" || ev.type === "drop") {
        var textContent = (ev.type === "paste" ? ev.clipboardData : ev.dataTransfer).getData('text');
        return !isNaN(textContent) && textContent.indexOf(".") === -1;
    } else if (ev.type === "keydown") {
        if (ev.ctrlKey || ev.metaKey) {
            return true
        };
        var keysToAllow = [8, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57];
        return keysToAllow.indexOf(ev.keyCode) > -1;
    } else {
        return true
    }
}
<input type="number" placeholder="paste numbers only" name="" onpaste="return isNumber(event)" onkeydown="return isNumber(event)" ondrop="return isNumber(event)">

Remove the second condition if && pasteContent.indexOf(".")===-1 if you want to allow integer as well as floating point numbers. but allowing floating point number will cause a problem if you already have a floating point number, so it will be invalid number with two "."

Below is a solution using a timeout.

<!DOCTYPE html>
<html>
<body>

<input type="text" id="inputText" onpaste="return isNumber(event);" onkeypress="return isNumber(event)"  value="" size="40">

<p id="demo"></p>

<script>
 function isNumber(evt) {
      evt = (evt) ? evt : window.event;
      var element  = document.getElementById("inputText");
      var initialValue  = element.value;
      if(evt.type == "paste"){
      	  setTimeout(function() {
          	var value  = element.value;
        	var pastedText = value.replace(initialValue,"");
            if(!isNaN(pastedText)){
            	element.value = value;
            }else{
            	element.value = initialValue;
            }
          }, 2);         
      }
      var charCode = (evt.which) ? evt.which : evt.keyCode;
      if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
      }
      return true;
    }
</script>

</body>
</html>

本文标签: