Monday, October 24, 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.


<html>
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script type="text/javascript">
$(function () {
debugger;
$('#txtDatePicker').datepicker()
});
</script>
</head>
<body>
<asp:TextBox ID="txtDatePicker" runat="server" ClientIDMode="Static">
</asp:TextBox>
</body> 
</html>

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;">
</asp:TextBox>

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

No comments:

Post a Comment