tags:

views:

190

answers:

6

I'm getting no Javascript errors, but the code does nothing...

function registerValidator(target, validator) {
    var element = document.getElementById(target);
    if (element) {
        var validationID = element.id + "_validationResult";
        var validationSpan = document.createElement('span');
        validationSpan.id = validationID;

        element.parentNode.appendChild(validationSpan);

        element.onkeyup = function() {
            var result = validator(element);

            if (result.ok) {
                validationSpan.innerHTML = '<img src="/media/valid.gif" width="12" />';
            } else {
                validationSpan.innerHTML = '<img src="/media/invalid.gif" width="12" />';
                if (result.message) {
                    validationSpan.innerHTML += '<br />' + result.message;
                }
            }
        };
        alert(1);
    }
}

window.onload = function() {
    registerValidator('username', function(element) {
        var result = new Object();
        alert('validate');
        if (element.value.length >= 4) {
            result.ok = true;
        }
        else {
            result.ok = false;
            result.message = "Too short";
        }
        return result;
    });
    alert(2);
}

The two alerts (1 and 2) are triggered correctly, but the 'validate' alert is never triggered. The function is used for the following element:

<input type="text" name="username" id="username" />

I have tried this in Google Chrome, Firefox 3 and Internet Explorer 8.

A: 

your element looks malformed. Did you actually have three quotes after the id= ?

djsadinoff
Oops, no I did not. It was an error during the copy-pasta'ing. (From removing the value part).
Aistina
A: 

The onkeyup event handler function needs to take a single argument:

element.onkeyup = function(evt) {

See the Mozilla docs.

jwoolard
having the event parameter is _not_ required. The browser will however _supply_ the event parameter when calling the event handler.
Jonathan Fingland
Right you are Jonathan - I learn something new every day!
jwoolard
+3  A: 

Everything is working fine here. see test page at http://ashita.org/StackOverflow/validator.html


You are currently using element.onkeyup which could be getting clobbered by another script.

try using

if (element.addEventListener)
    element.addEventListener("keyup", validator,false);
else if (element.attachEvent) 
    element.attachEvent("onkeyup", validator);

References:

Jonathan Fingland
A: 

I don't think there is a problem with the script. I have the following html working perfectly for me in IE8 and FF with your script,

<html>
<script>
function registerValidator(target, validator) {
    var element = document.getElementById(target);
    if (element) {
        var validationID = element.id + "_validationResult";
        var validationSpan = document.createElement('span');
        validationSpan.id = validationID;

        element.parentNode.appendChild(validationSpan);

        element.onkeyup = function() {
            var result = validator(element);

            if (result.ok) {
                validationSpan.innerHTML = '<img src="/media/valid.gif" width="12" />';
            } else {
                validationSpan.innerHTML = '<img src="/media/invalid.gif" width="12" />';
                if (result.message) {
                    validationSpan.innerHTML += '<br />' + result.message;
                }
            }
        };
        alert(1);
    }
}

window.onload = function() {
    registerValidator('username', function(element) {
        var result = new Object();
        alert('validate');
        if (element.value.length >= 4) {
            result.ok = true;
        }
        else {
            result.ok = false;
            result.message = "Too short";
        }
        return result;
    });
    alert(2);
}
</script>
<body>
<input type="text" id="username" name="username"/>
</body>
</html>

There might be someother script trying to modify the onkeyup event handler for the element.

Gee
A: 

I've tested it in Chrome and IE8 locally and it works absolutely fine. You're not setting the window.onload event elsewhere in your code are you? What about loading other libraries like JQuery or Prototype?

If you're receiving absolutely nothing at all I'd expect that something is overriding the onload or onkeyup events.

Andy E
A: 

Oops. I forgot there's another textbox with id "username" in the header of the page, to allow people to login. Obviously the validation was working fine there ¬_¬

Aistina