Wednesday, September 18, 2013

How to disable cut, copy, paste on a textbox using jquery

First declare a textbox & then applying the following jquery method like this:

  <input type="text" name="txtName" id="txtName" />

<script type="text/javascript">
    $(document).ready(function () {
        $('#txtName').bind('cut copy paste', function (e) {
            // prevent the default operation
            e.preventDefault();
            // warn the user
            alert("Paste disabled in this textbox");
        });
    });
</script>

Sunday, September 15, 2013

How to make a textbox numeric only using jquery

Firstly declare a input textbox & then applying the following regex on the keyup event of textbox like this:

<input type="text" name="txtSalary" id="txtSalary" />

 $('#txtSalary').keyup(function (e) {
        if (/\D/g.test(this.value)) {
            // Filter non-digits from input value.
            this.value = this.value.replace(/\D/g, '');
        }
    });