Truncate everything after first 120 characters and refresh

297 Views Asked by At

I'm quite new to UI development.

I have a textfield. (input) where the user enters a string for SMS. I want to discard all the characters which are more than 120. I have written code for this one.

Problem: if the user enters more than 120 characters, how do I remove the last extra character? (I shouldn't let him to enter more than 120)

How do I do it?

this is my code for input change:

    onSMSMessageInputChange : function(){
        var smsText=$(this.ui.smsInput).val().substring(0,120); //get entered text
        this.model.setProp("smsMessages",smsText); //set it to a model

    },
3

There are 3 best solutions below

0
On BEST ANSWER

You can use a maxlength attribute (see docs: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea) to limit the amount of characters that a person can enter.

<textarea maxlength="120"></textarea>

0
On

HTML has a maxlength attribute. You could use that for your input textbox.

0
On

Just use the maxlength attribute for input. No other code for input change is required.

<input type="text" name="_____" maxlength="120">