0

is it possible to limit width of input text based on the number of character that will fit on the field without declaring the maxlength, regardless of the font type and font size used? I just wanted the user to be able to type inside the input field without text overflow.

cyiana
  • 1
  • 2
    How does limiting the user's input options based on their browser's font-setting make any kind of UI sense? How are they meant to know that if they want to add more information they've got reduce their font-size? – David Thomas Jul 12 '14 at 12:47
  • example: what if the the input text is for "Reason: ________________" the user has to input within the text line...it wont allow them to text beyond the line...if they have long answer, they could rephrase it or summarize it as long as it would fit in one line...otherwise it type longer, it will get cut when it get printed and the user will complain why the text was cut off...so to avoid that, it is better to not let them type beyond the text line...anyway the width is estimated on the possible answers... – cyiana Jul 12 '14 at 13:00

1 Answers1

0

Try the below code:

jQuery(document).ready(function($) {
    var max = 4;
    $('textarea.max').keypress(function(e) {
        if (e.which < 0x20) {
            // e.which < 0x20, then it's not a printable character
            // e.which === 0 - Not a character
            return;     // Do nothing
        }
        if (this.value.length == max) {
            e.preventDefault();
        } else if (this.value.length > max) {
            // Maximum exceeded
            this.value = this.value.substring(0, max);
        }
    });
}); //end if ready(fn)

For more details refer to this

Community
  • 1
  • 1
ak0053792
  • 523
  • 1
  • 5
  • 18
  • I tried this, this is good when you declare the max length of character..the question is, what about we don't really know how many character will fit in the line? for example, the text line may fit 20 letter L (small caps) which is equivalent to 4 Ws....so we really can't say how many characters can fit within the line... – cyiana Jul 12 '14 at 12:50