Migrate
07-22-2004, 04:18 AM
Good Morning.
I'm trying to create a input form element that accepts only numbers.
To accomplish this I add a event listener in Javascript to the input element (by using attachEvent for IE6 and addEventListener for Mozilla), but the function return value is ignored in Mozilla Firefox 0.8 (it works in IE6).
If instead of using the addEventListener I use directly the onkeypress event in the element it works correctly ( onkeypress="return mask(event)").
Can anyone tell me who to overcome this problem?
Here is the code:
<label for="myInput">Atacht Event in Javascript</label>
<input type="text" id="myInput" name="myInput">
<br>
<label for="myInput">Atacht Event in input</label>
<input type="text" id="myInput2" name="myInput2" onkeypress="return mask(event)">
<script>
attachEvent();
function attachEvent() {
var inputElem = document.getElementById("myInput");
// for IE 6
if (document.all) {
inputElem.attachEvent("onkeypress", mask);
}
// for Mozilla
else {
inputElem.addEventListener("keypress", mask, false);
}
}
var reValidChars = /\d/;
function mask(objEvent) {
var iKeyCode, strKey, objInput;
iKeyCode = objEvent.which;
if (document.all) {
iKeyCode = objEvent.keyCode;
objInput = objEvent.srcElement;
} else {
iKeyCode = objEvent.which;
objInput = objEvent.target;
}
strKey = String.fromCharCode(iKeyCode);
if (!reValidChars.test(strKey)) {
return false;
}
}
</script>
Thanks in advance.
I'm trying to create a input form element that accepts only numbers.
To accomplish this I add a event listener in Javascript to the input element (by using attachEvent for IE6 and addEventListener for Mozilla), but the function return value is ignored in Mozilla Firefox 0.8 (it works in IE6).
If instead of using the addEventListener I use directly the onkeypress event in the element it works correctly ( onkeypress="return mask(event)").
Can anyone tell me who to overcome this problem?
Here is the code:
<label for="myInput">Atacht Event in Javascript</label>
<input type="text" id="myInput" name="myInput">
<br>
<label for="myInput">Atacht Event in input</label>
<input type="text" id="myInput2" name="myInput2" onkeypress="return mask(event)">
<script>
attachEvent();
function attachEvent() {
var inputElem = document.getElementById("myInput");
// for IE 6
if (document.all) {
inputElem.attachEvent("onkeypress", mask);
}
// for Mozilla
else {
inputElem.addEventListener("keypress", mask, false);
}
}
var reValidChars = /\d/;
function mask(objEvent) {
var iKeyCode, strKey, objInput;
iKeyCode = objEvent.which;
if (document.all) {
iKeyCode = objEvent.keyCode;
objInput = objEvent.srcElement;
} else {
iKeyCode = objEvent.which;
objInput = objEvent.target;
}
strKey = String.fromCharCode(iKeyCode);
if (!reValidChars.test(strKey)) {
return false;
}
}
</script>
Thanks in advance.