mein Code:
function checkFormFieldIsPositiveInt(formFieldID)
{
if((formFieldID == "[object KeyEvent]") || (formFieldID == "[object KeyboardEvent]"))
{
formFieldID = formFieldID.target.id;
}
if(formFieldID == "[object Event]")
{
formFieldID = formFieldID.srcElement.id;
}
for(i = 0; i < document.getElementById(formFieldID).value.length; ++i)
{
alert('Event ausgelöst');
if((document.getElementById(formFieldID).value.charAt(i) == ".") || (document.getElementById(formFieldID).value.charAt(i) == ",") || ((document.getElementById(formFieldID).value.charAt(i) < "0") || (document.getElementById(formFieldID).value.charAt(i) > "9"))) {
document.getElementById(formFieldID).style.border = "thin solid #FF0000";
break;
}
else if((document.getElementById(formFieldID).value.charAt(i) == " ") || (document.getElementById(formFieldID).value.charAt(i) == "") || (document.getElementById(formFieldID).value == ""))
{
document.getElementById(formFieldID).style.border = "thin solid #000000";
}
else
{
document.getElementById(formFieldID).style.border = "thin solid #000000";
}
}
}
arrayEventListeners = new Array("field1#keyup#checkFormFieldIsPositiveInt","field2#keyup#checkFormFieldIsPositiveInt");
function addEventListeners(arrayEventListeners) // v. 1.0.3
{
// arrayEventListeners: "elementID#shortEvent#funtionName
for(var i in arrayEventListeners)
{
var arrayEventListener = arrayEventListeners[i].split("#");
var functionReference = window[arrayEventListener[2]];
if (document.addEventListener)
{
// allgemeine Ereignisbehandlung nach W3C
document.getElementById(arrayEventListener[0]).addEventListener(arrayEventListener[1], functionReference, false);
}
else if (document.attachEvent)
{
// Spezielle Version für IE
document.getElementById(arrayEventListener[0]).attachEvent("on" + arrayEventListener[1], functionReference);
}
else
{
alert("Fehler");
}
}
}
addEventListeners(arrayEventListeners);
dazu die HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Unbenanntes Dokument</title>
</head>
<body>
<form action="" method="post" name="form">
<input id="field1" />
<input id="field2" />
</form>
<script type="text/javascript" src="./js.js"></script>
</body>
</html>