Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: TextArea MaxLength Enforcement - jQuery

Compare Page Revisions



« Older Revision - Back to Page History - Current Revision


Page Revision: Wed, Jan 19, 2011, 2:19 PM


The following JavaScript function, which is dependent on jQuery, enforces the maxlength attribute for all textarea elements on the current page.

{copytext|js}
function enforceTextAreaMaxLen() {

    $('textarea[maxlength]').keyup(function () {

        var textbox = $(this);
        var maxlength = textbox.attr('maxlength');

        if (maxlength != undefined) {

            var x = parseInt(maxlength);
            var s = textbox.val();
            if (s != null && s.length > x) {
                textbox.val(s.substring(0, x));
            }
        }
    });
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.