Question

I have a asp:TextBox and I need to validate that only accept characters.

Textbox

<asp:TextBox runat="server" id="txt" onkeypress="return AllowAlphabet(event)" />

Javascript

function AllowAlphabet(e)
{
  isIE = document.all ? 1 : 0
  keyEntry = !isIE ? e.which : event.keyCode;
  if (((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry == '46') || (keyEntry == '32') || keyEntry == '45')
     return true;
  else
{
    alert('Please Enter Only Character values.');
    return false;
      }
}

It works good in chrome, firefox but doesn't work in IE8.

what can I do make to it work?

No correct solution

OTHER TIPS

Wouldn't this work for you?

function AllowAlphabet(e) {
    e = e || window.event;
    var keyEntry = e.which || e.keyCode;
    var bIsCharacter =
        (keyEntry >= 65 && keyEntry <= 90) ||
        (keyEntry >= 97 && keyEntry <= 122) ||
        (keyEntry == 46) ||
        (keyEntry == 32) ||
        (keyEntry == 45);
    return bIsCharacter;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top