Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: [DHTML] Form Field Limiter

  1. #1
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Post [DHTML] Form Field Limiter

    1) CODE TITLE: Form Field Limiter

    2) AUTHOR NAME/NOTES: Twey, http://www.twey.co.uk/

    3) DESCRIPTION: An object to control limited-length form fields, with a great deal more flexibility than that offered by the HTML maxlen attribute.

    4) URL TO CODE: http://www.twey.co.uk/?q=limiter
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Nice script. This will be useful, considering alot of people ask how to do this.
    Wouldn't the basic object.substring(0,maxlength); work though?
    - Mike

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Hey, it wasn't my idea, I just rewrote this piece of crud
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Err... what's wrong with:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>max length</title>
    <script type="text/javascript">
    function loadMax(el,max) {
      var end = document.createElement("div");
      el.load = function() {
        var charc = max-el.value.length,str=document.createTextNode("There are "+charc+" characters remaining.");
        while (f=end.firstChild) end.removeChild(f);
        end.appendChild(str);
        }
      el.load();
      el.parentNode.appendChild(end);
      el.onkeydown = function() {
        el.value=el.value.substring(0,max);
        el.load();
        }
      }
    </script>
    </head>
    <body>
    <form name="myForm">
    	<input type="text" name="foo">
    </form>
    <script type="text/javascript">
    loadMax(document.forms['myForm'].foo,10);
    </script>
    </body>
    </html>
    //EDIT: created a work-around for the weird positioning:
    Code:
    <form name="myForm">
    	<div>
    		<input type="text" name="foo">
    	</div>
    	<div>
    		<textarea name="test"></textarea>
    	</div>
    </form>
    Last edited by mburt; 04-07-2007 at 07:19 PM.
    - Mike

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Several big fat memory leaks in IE, for a start Also, doesn't offer any more flexibility than the original, it's still difficult to position the output correctly, doesn't give any options to style the output, doesn't indicate the length limit to non-JS browsers, &c. Most of those aren't problems, of course, per se, but just features found in mine and not that one... why'd you rewrite it again? Go and find your own antiquated script to do up
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Technically I didn't rewrite it... I started from scratch (not incredibly hard, by the way). I'll have a closer look at yours, but to be honest, it looked exactly the same as the old one.
    I'm just saying mine is simpler, nothing less.

    And what exactly is this "big fat" security leak in IE?
    Last edited by mburt; 04-07-2007 at 07:19 PM.
    - Mike

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The effect is the same (with a couple of optional extras), but it's achieved using better code: "rewritten."
    And what exactly is this "big" security leak in IE?
    You've got a DOM element trapped in the onkeydown() and load() closures, and also referenced circularly by both: el references load references el.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    and also referenced circularly by both: el references load references el.
    Actually, it doesn't.
    el.load():
    Code:
        var charc = max-el.value.length,str=document.createTextNode("There are "+charc+" characters remaining.");
        while (f=end.firstChild) end.removeChild(f);
        end.appendChild(str);
    All it does is set the charc variable, nothing else. It doesn't once use el as a reference, it's just an "updating" function later called in the onkeydown() handler. I don't know how this would end up in a loop. Could you clarify? (sorry if I'm being annoying, but I don't really understand)
    - Mike

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It doesn't matter, it's a closure, it has access to every variable in the scope in which it was created, which includes el.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Okay... makes sense. Doesn't seem to have much of a negative effect though.
    - Mike

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
  •