Saturday, December 24, 2011

Validating a Textbox using Javascript in asp.net

Validate Textbox, not allowing string to be entered 

Example


in .aspx


<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>


in .cs 


write in page_load Event


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



No comments:

Post a Comment