Sunday, October 23, 2016

Asp.Net : Best way to restrict "Backspace" and "Delete" key on inputbox

Here i am phasing problem to restrict "Backspace" and "Delete" key on textbox. I am using JQuery calendar with the textbox. Here i want to restrict user input from keyboard to reduce the validation to check date.

 To add the datepicker to your textbox add below reference of the JQuery datepicker.

<link rel="stylesheet" href="//">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src=""></script>
<script src=""></script>

<script type="text/javascript">
$(function () {
<asp:TextBox ID="txtDatePicker" runat="server" ClientIDMode="Static">

In above code user are free to edit after datepick. User can change, copy, paste and delete the textbox value. To restrict the keyboard input by the user, here i use

onkeypress="return false;" onpaste="return false;". 

<asp:TextBox ID="txtDatePicker" runat="server" ClientIDMode="Static" 
  onkeypress="return false;" onpaste="return false;"></asp:TextBox>

Above code restrict all key but, there is "Delete" and "Backspace" key is not restrict. To restrict both key, here i am using 

onkeydown="return false;" 

<asp:TextBox ID="txtDatePicker" runat="server" ClientIDMode="Static" 
onkeydown="return false;" onkeypress="return false;" onpaste="return false;">

Now this is done, to restrict all key with "Delete" and "Backspace" key.

No comments:

Post a Comment