How to disable the insert option for the string part, but not for the numeric ones?

I have textboxone that only accepts numbers. I made this piece, but the problem is that I can not turn off the insert option textboxie The, onPaste="return false;". Thus, a string can be entered through the insert parameter. So, how to disable the insert option for the string part, but not for the numeric?

function OnlyNumbers()
{
if (event.keyCode <= 46 || event.keyCode > 57 || event.keyCode == 47) event.returnValue = false;
}

<asp:TextBox ID="txtRecApr" class="txt" autocomplete="off" 
onKeyPress="OnlyNumbers()" onfocus="javascript:this.select();" 
runat="server" Width="60px" MaxLength="5" AutoPostBack="True" 
onPaste="return false;" ontextchanged="txtRecApr_TextChanged">
</asp:TextBox>
+4
source share
1 answer

Try this script. Hope this helps you.

<script type="text/javascript">  

            $(document).ready(function () {  
                var keyDown = false, ctrl = 17, vKey = 86, Vkey = 118;  

                $(document).keydown(function (e) {  
                    if (e.keyCode == ctrl) keyDown = true;  
                }).keyup(function (e) {  
                    if (e.keyCode == ctrl) keyDown = false;  
                });  

                $('input[type=text]').on('keypress', function (e) {  
                    if (!e) var e = window.event;  
                    if (e.keyCode > 0 && e.which == 0) return true;  
                    if (e.keyCode) code = e.keyCode;  
                    else if (e.which) code = e.which;  
                    var character = String.fromCharCode(code);  
                    if (character == '\b' || character == ' ' || character == '\t') return true;  
                    if (keyDown && (code == vKey || code == Vkey)) return (character);  
                    else return (/[0-9]$/.test(character));  
                }).on('focusout', function (e) {  
                    var $this = $(this);  
                    $this.val($this.val().replace(/[^0-9]/g, ''));  
                }).on('paste', function (e) {  
                    var $this = $(this);  
                    setTimeout(function () {  
                        $this.val($this.val().replace(/[^0-9]/g, ''));  
                    }, 5);  
                });  
            });  
        </script>

EDIT:

. , , . . , replace(/[^0-9]/g, ''). , 0-9 . .

+1

Source: https://habr.com/ru/post/1599810/


All Articles