Saturday, July 28, 2007

JavaScript to Allow Only Numbers in a TextBox

Write this in the HTML Part

Please Inculde the JavaScript Tag and place the below function in it.

function fnAllowNumeric()
{
if (event.keyCode < 48 || event.keyCode > 57 ) event.returnValue = false;
}

Close the Script Tag.

Write this in the CodeBehind Part

and in the Page_Load write like this
Page_Load
{
this.txtNumeric.Attributes["OnKeyPress"] = "Javascript:return fnAllowNumeric()";
}

0 comments: