jQuery maxLength input


/ Published in: jQuery
Save to your folder(s)

jQuery plugin that let you specify a maximum length on a text field and optionaly specify the change the focus when that maximum length has been reach. Nice when making multiple input text to create a telephone form.
Example: $("#telephone_1").maxLength(3, "#telephone_2");


Copy this code and paste it in your HTML
  1. (function($){
  2. $.fn.extend({
  3. maxLength: function(length, nextInput) {
  4. return this.each(function(i, el) {
  5. $(el).keyup(function() {
  6. if($(this).val().length >= length) $(this).val( $(this).val().substring(0, length) );
  7. if(nextInput != null) if($(this).val().length >= length) $(nextInput).focus();
  8. })
  9. })
  10. }
  11. });
  12. })(jQuery);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.