Thursday, 15 November 2012

Allow User to Enter Only Numbers in a Textbox Using Javascript


Allow User to Enter Only Numbers in a Textbox Using Javascript




<script type="text/javascript">

function isNNumeric(e)

{

var isNN = (navigator.appName.indexOf("Netscape")!=-1);

var keyCode = (isNN) ? e.which : e.keyCode;

//alert(keyCode);

if (isNN)

{

if (keyCode == 0)

return true;

}

if((keyCode>47&&keyCode<58)||(keyCode==8)||(keyCode==9))

{

return true;

}

else

{

if (e.returnValue)

{

e.returnValue = false;

return false;

}

else if (e.preventDefault)

{

e.preventDefault();

return false;

}

this.event.returnValue = false;

return false;          

}

}

</script>


Then call the above function in the textbox Onkeypress Event as follows:

 <asp:TextBox ID=”txt_maxage” Onkeypress=”return isNNumeric(event)” runat=”server”></asp:TextBox>

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top