Onkeydown , I run the following JavaScript:
function ThisOnKeyDown(el) { if (el.title == 'textonly') { !(/^[A-Za-zÑñ-\s]*$/i).test(el.value) ? el.value = el.value.replace(/[^A-Za-zÑñ-\s]/ig, '') : null; } if (el.title == 'numbersonly') { !(/^[0-9]*$/i).test(el.value) ? el.value = el.value.replace(/[^0-9]/ig, '') : null; } if (el.title == 'textandnumbers') { !(/^[A-Za-zÑñ0-9-\s]*$/i).test(el.value) ? el.value = el.value.replace(/[^A-Za-zÑñ0-9-\s]/ig, '') : null; } }
One of these three header attributes is assigned to the various input fields on the page. The code works until the invalid characters are correctly erased, but not until the next character is entered. I want to find a way to simply discard invalid input in the first place. I appreciate your help!
Change I create events globally. Here is how I do it:
function Globalization() { var inputs = document.getElementsByTagName('input'); for (i = 0; i < inputs.length; i++) { inputs[i].onfocus = createEventHandler( ThisOnFocus, inputs[i]); inputs[i].onblur = createEventHandler( ThisOnBlur, inputs[i]); inputs[i].onkeydown = createEventHandler( ThisOnKeyDown, inputs[i]); inputs[i].onkeyup = createEventHandler( ThisOnKeyUp, inputs[i]); } }
Globalization() is executed by body.onload
Therefore, a typical input field contains HTML without calling functions like this:
<input id="AppFirstName" style="width: 150px;" type="text" maxlength="30" title="textonly"/>
source share