Hii

I am doing a project dealt with sending SMS ..
So i have to control the length of the message to be sent ..
But for textbox of Multiline type did not allowing the MaxLength property ..
Plz help me

Dani AI

Generated

A practical, modern solution builds on ’s client-side idea but makes it robust for typing, pastes and older browsers: add the HTML5 maxlength as a progressive enhancement, use the input event for live enforcement and a remaining-character display, and always enforce the limit server-side before sending the SMS (client checks are convenience only). This addresses ’s need for a reliable length control for SMS.

Example (HTML + plain JavaScript):

<textarea id="message" maxlength="160" rows="5" cols="50"></textarea>
<div>Remaining: <span id="remaining">160</span></div>
<button id="send">Send</button>
<script>
(function(){
  var ta = document.getElementById('message');
  var rem = document.getElementById('remaining');
  var max = 160;

  function update(){
    if (ta.value.length > max) ta.value = ta.value.slice(0, max);
    rem.textContent = max - ta.value.length;
  }

  ta.addEventListener('input', update, false); // handles typing and paste
  update();
})();
</script>

Server-side validation (example in C#) — enforce the same rule on the server to prevent bypass:

if (string.IsNullOrEmpty(message) || message.Length > 160)
{
    // reject or return validation error before sending SMS
}

Notes and troubleshooting:

  • onchange fires on blur; input gives immediate feedback and handles paste/IME.
  • HTML5 maxlength works in modern browsers but keep the JS fallback for older ones.
  • SMS specifics matter: typical GSM 7-bit messages allow 160 chars, UCS-2 (non-GSM chars/emoji) 70 chars; concatenated messages reduce per-part capacity (commonly 153 / 67). For billing/segmenting accuracy, use the SMS gateway’s API or a proven library to calculate segments and encoding before sending.

Recommended Answers

All 2 Replies

why not use a javascript function fired by the onchange event to check the length and prevent additonal entry if the length exceeds the max allowable

why not use a javascript function fired by the onchange event to check the length and prevent additonal entry if the length exceeds the max allowable

Thanks a lot Mr. sedgey

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.