I am trying to put a restriction in my text boxes to enter only numbers, by adding event listener for each text boxes id having a pattern.
$(document).ready(function() {
var numOnlyTextBoxes = 'input[id$="NumOnly"]';
$(numOnlyTextBoxes).each(function() {
alert(this.id);
this.addEventListener("input", function(e) {
var val = this.value,
rx = /[^\d]/g;
if (rx.test(val)) {
var pos = this.selectionStart;
this.value = val.replace(rx, "");
this.selectionStart = pos;
this.selectionEnd = pos - 1;
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<INPUT id="firstNumOnly" TYPE="text" NAME="">
<INPUT id="secondNumOnly" TYPE="text" NAME="">
<INPUT id="thirdNumOnly" TYPE="text" NAME="">
I used this addEventListener approach to ensure it is validated in copy paste as well. First alert box is displayed and after that 2 more alerts are not coming. And all Text boxes are working as expected only when i launch IE developer tool.
As specified in this link Why does JavaScript only work after opening developer tools in IE once? , i am not using any console and also i tried putting $.ajax({cache: false, ...})
in my code but even then it is not working. Browser: IE11
But a plain HTML or the JS fiddle working
I got error like .addEventListener is not supported, for that reason my onload function has below code as well
/* IE11 Fix for SP2010 */
if (typeof(UserAgentInfo) != 'undefined' && !window.addEventListener)
{
UserAgentInfo.strBrowser=1;
}