Setting the maximum size for textbox in javascript

Hi! I want to set the maximum size for the user to input the text.
I have:


var box = document.createElement("input");
box.type ="text";
box.size = 7;
box.maxlength = 7;

but it doesn’t work. The user can still input more than 7 characters. Please help.

Thanks.


box.setAttribute('maxlength', 7);

Thanks for your answer. But it doesn’t work in IE. Works only in Firefox.

Is there anyway to limit it in IE?

Thanks.

Yes, you may have to get more extreme.


box.onkeyup = function () {
    if (this.value.length > 7) {
        this.value = this.value.substring(0,7);
    }
};

thanks

Hi,

Use:

box.maxLength = 7;

Instead of:

box.maxlength = 7;