SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code JavaScript:
    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.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Code javascript:
    box.setAttribute('maxlength', 7);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your answer. But it doesn't work in IE. Works only in Firefox.

    Is there anyway to limit it in IE?

    Thanks.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Yes, you may have to get more extreme.

    Code javascript:
    box.onkeyup = function () {
        if (this.value.length > 7) {
            this.value = this.value.substring(0,7);
        }
    };
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Use:

    box.maxLength = 7;

    Instead of:

    box.maxlength = 7;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •