Disable copy paste of alphabets in text field using jquery

后端 未结 6 1846
遇见更好的自我
遇见更好的自我 2021-01-14 05:33

In my project i have text field that only take numeric value.but when I copy an alphabets using ctl+c and paste using ctl+v it will allow the alphabets in the text field.So

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-14 06:07

    I have a funny workaround for your problem. For paste event you may use the following code:

    $("input").on("paste", function(e) {
        var that = this;
        that.style.color = "#fff";      // field background color
        setTimeout(function() {
            that.value = that.value.replace(/\D/g, "");
            that.style.color = "#000";  // normal field font color
        }, 100);
    });​
    

    DEMO: http://jsfiddle.net/dgWDX/

提交回复
热议问题