Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi, All.

Please help me. I need to disable alt + key codes in the textbox to restrict some special characters like %.

Thank you!
Posted
Comments
Sergey Alexandrovich Kryukov 2-Nov-15 1:40am    
Special characters? Never heard of any. How % is special? How is it related to Alt+Key? I don't think the question makes any sense, but if it does, please clarify.
—SA

C#
$('selector').keypress(function(e) {
  console.log(e.shiftKey);
  if ((.altKey) {
    e.preventDefault();
    alert('Disallowed');
  }
});
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Nov-15 1:43am    
It's "e.altKey". Basically, you are right, but what action on an input control will it prevent? The inquirer did not explain it.
—SA
You just need to disable only the alt key, rest is a combination to perform specific operation:
JavaScript
$(".txt").keypress(function(e) {
    if(e.altKey){
        return false;  // or e.preventDefault();
    }
});


-KR
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Nov-15 1:44am    
You are perfectly right, but what action on an input control will it prevent? The inquirer did not explain it.
—SA
XML
<script language="javascript" type="text/javascript">
function getKeyCodeEvent(ev) {
 var code = (document.all) ? event.keyCode:ev.which;
 var alt = (document.all) ? event.altKey:ev.modifiers & Event.ALT_MASK;
if (document.all)
        {
        if (alt && code==115) //Example ALT+F4
        {
          try{
            //your requirements
            }catch(e){
          }
        }
        }

}
</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900