SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery to limit text box characters

    How do I use jQuery to limit my text box characters to 20?
    I tried the below with no luck:

    $('input[name$="myIDname"]').limit('20');
    $('#myIDname').limit('20');

    Thanks

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    You don't really need to do this in JS, just add a "maxlength" attribute: <input type="text" maxlength="70"> or <textarea maxlength="50"></textarea>.

    The maxlength attribute for <input type="text"> is supported across all modern browsers.
    However the maxlength attribute is new for the <textarea> tag in HTML5 and is not supported in Internet Explorer 9 (and earlier versions), or in Opera.

    Also, please be aware that it would be relatively trivial to circumvent any client-side restrictions you place on input length and it is best to check again on the server, that the input matches the desired format.

    If you need any more help with this, just let us know.


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
  •