how to cal key press event of textbox in asp.net

Posted by Shanky11 under ASP.NET on 9/28/2012 | Points: 10 | Views : 16258 | Status : [Member] | Replies : 6
how to cal key press event of textbox in asp.net



my function is as below
<script type="text/javascript" language="javascript">
function validateNumbersOnly(e) {
var unicode = e.charCode ? e.charCode : e.keyCode;
if ((unicode == 8) || (unicode == 9) || (unicode > 47 && unicode < 58)) {
return true;
}
else {

window.alert("This field accepts only Numbers");
return false;
}
}
</script>




Responses

Posted by: Jigarbagadai on: 9/28/2012 [Member] Starter | Points: 25

Up
0
Down
user below code to call that function in keypress event. To Use below code you need to add jquery reference file.

$(document).ready(function () {
$(<%=TextBox1.ClientID%>).keypress(
function(event){ validateNumbersOnly(event);}
);
});

Jigar Bagadai

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Solijoseph on: 9/28/2012 [Member] Starter | Points: 25

Up
0
Down
<input id="txtName" runat="server" maxlength="32" onkeypress="return runScript(event)">

<script>
function runScript(e) {
if (e.keyCode == 13) { // Invoke when press Enter Key

return false;
}
}
</script>


Thanks & Regards
Solimon Joseph

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 9/28/2012 [Member] Starter | Points: 25

Up
0
Down
we can do in jquery it is very simple and lightwight process.
follow Jigar Bagadai code it is good code


Thanks & Regards
Hari

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samirg on: 9/28/2012 [Member] Starter | Points: 25

Up
0
Down
Use the following attribute on the input tag, onkeypress="validateNumbersOnly(event)".

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/28/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
refer this url

http://www.dotnetfunda.com/codes/code3290-how-to-fire-javascript-and-jquery-in-textbox-keypress-event.aspx




Mark as Answer if its helpful to you

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/28/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hu
Please mark as answer if it helpful to you.


Mark as Answer if its helpful to you

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response