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

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.