Monday, 24 October 2011

Allow only Alphanumeric in the Textbox using Javascript

Script:
<script type = "text/javascript">
   
var isShift=false;
         
      function isAlphaNumeric(keyCode)
      {    
            if (keyCode == 16)
                isShift = true;

            return (((keyCode >= 48 && keyCode <= 57) && isShift == false) || (keyCode >= 65 && keyCode <= 90) || keyCode == 8 ||  keyCode == 35 ||  keyCode == 36 || keyCode == 37 || keyCode == 39 || keyCode == 46 || (keyCode >= 96 && keyCode <= 105));

                        // 8  - Backspace Key
            // 35 - Home Key
            // 36 - End Key
            // 37 - Left Arrow Key
            // 39 - Right Arrow Key
            // 46 - Del Key  
}    


function KeyUp(keyCode)
{
      if (keyCode == 16)
            isShift = false;
}

</script>

HTML:
<body>   
    <input type="text"  onkeydown="return isAlphaNumeric(event.keyCode);" onkeyup="KeyUp(event.keyCode);" />
</body>

2 comments: