faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 15 people (67%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How to block usr frm typing Non-Ascii chars like ,, (we use Alt + NumPad key combinations)

Apr 7th, 2008 23:58
ha mo, Colin Fraser, Krishna Chanda,


Using the onKeyPress event:
function cancelRefresh() {
  // Use the upper limit for alphanumeric key code: 
  if (window.event && window.event.keyCode >= 128) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
    return false;
  }
}
This takes care of the basic ASCII key codes. It allows users to  write 
and use the DEL key, but not the extended key codes. 
As an alternative, to access the extended key codes, the alt key is 
needed (unless the keyboard has been mapped to access the extended key 
codes). Try this:
function microsoftKeyPress() {
    var prefix = '';
    if (window.event.ctrlKey) 
         document.test.control.value = 'true';
     else
         document.test.control.value = '';
    if (window.event.altKey)
         document.test.alt.value = 'true';
     else
         document.test.alt.value = '';
    if (window.event.shiftKey)
         document.test.shift.value = 'true';
     else
         document.test.shift.value = '';
    document.test.key.value = window.event.keyCode;
}
I don't know where I got this code, or when, but it should still work. 
It can be called on an onkeyPress function, so if you trap the ALT key 
stroke then cancel it using the code above this, it should work. 
cheers,
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com