Thursday, July 14, 2011

Javascript, only numbers textbox, not allow copy/paste to it.

<asp:textbox id="TextBox1" oncopy="return false;" onpaste="return false;" runat="server"></asp:textbox>
Note that I attached event handlers to the element in the HTML declaration. This will work, but will produce a compiler warning. The proper way to do this for a server control is in the Page_Load handler of the server-side code like this:
 TextBox1.Attributes.Add("oncopy", "return false;");
 TextBox1.Attributes.Add("onpaste", "return false;");

-----------------------
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCodeif (charCode > 31 && (charCode < 48 || charCode > 57)) {

return false;
document.getElementById("charCode").value = ""; }
else
{
return true; }
}
mytextbox.Attributes.Add("onkeypress", "JavaScript:return isNumberKey(event);");

No comments:

Post a Comment