Wednesday, July 6, 2011

Validating the Textbox using Javascript

// Not allowing string to be entered in textbox accept number


<script type="javascript">
function allownumber(e)
{
var key=window.event ? e.keycode:e.which;
var keychar=String.fromCharCode(key);
var reg=new RegExp("[0-9.]");
if(key=="")
{
keychar=String.fromCharCode(key);


}
if(key==13)
{
key=8;
keychar=String.fromCharCode(key);
}
return reg.test(keychar);
}


</script>


//Code Behind 


 on page_load


TextBox1.Attributes.Add("onkeypress","javascript:return allownumber(event);");


//It will not allow user to enter string accept number

No comments:

Post a Comment