Dynamic Text Box with Numeric value validation.

Posted by Tamilmaran under C# category on | Views : 4637
In this article we deals with how to create the textbox dynamically and validate for numeric value only.

step 1:

place a empty panel in aspx page(Its name panel1).Add the Text box(Its name is NumTxt) in form load event by using the below code

step 2 :

place the below code in page load event.

protected void Page_Load(object sender, EventArgs e)
TextBox NumTxt= new TextBox();
NumTxt.Attributes.Add("onkeypress", "return NumericOnly(window.event);");

NumTxt.Attributes.Add("onkeypress", "return NumericOnly(window.event);");
The above mentioned c# statement links the client onkeypress event with the NumericOnly java script function

Add the below script function in script area in aspx page

<script language ="javascript"  type ="text/javascript" >
function AllowNumeric(objEvent)

var iKeyCode, strKey;

var iKeyCode = objEvent.keyCode || objEvent.which;
// adjust the ascii value according to u r requirement
if(!(iKeyCode>=48 && iKeyCode<=57))
alert ("plz Enter Numeric value only");
return false;

ikeycode value consist the ascii value for the keyed character. if we keyed other than the ascii value of greater than 47 and less than 58 means it w'nt accept. It produces the alert message and won't accept the keyed value.

Comments or Responses

Login to post response