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

TextArea MaxLength Enforcement - jQuery

RSS
Modified on Fri, Aug 19, 2011, 1:15 PM by Administrator Categorized as JavaScript, jQuery, and Angular
The following JavaScript function, which is dependent on jQuery, enforces the maxlength attribute for all textarea elements on the current page. This is necessary for Internet Explorer, since it seems to be the only browser which has the bug of not enforcing this attribute.

{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-2018, Patrick Jasinski.