Sirwan Afifi
Sirwan Afifi

Reputation: 10824

validate length of textarea

hi i am working on SMS panel and i want to check to length of text area now i am using these code :

        $("#txt").keydown(function () {
            var len = $(this).val().length;
            $("#lbl").text(len);
            if (len <= 70) {
                $("#lbl").text("One SMS");
                if (confirm("more than one sms are you sure?")) {
                    $("#lbl").text(len);
                }
            }
            else if (len >= 71 && len <= 133) {
                $("#lbl").text("two SMS");
                if (confirm("more than two sms are you sure?")) {
                    $("#lbl").text(len);
                }
            }
            else if (len > 134 && len <= 199) {
                $("#lbl").text("three SMS");
                alert("more than three sms are you sure?");
            }
        });

i want when i paste text into textArea alert is displayed and select no cant input anything

thanks in your advise?

Upvotes: 0

Views: 532

Answers (2)

Raghurocks
Raghurocks

Reputation: 925

but it's not working perfectly,

One of the error I could see that is not making it work perfectly is, you have used the keydown event to work on finding the length, so if you copy and paste there is no Keydown event happening so you cannot find the length there, so you can write the same function in the onchange event of the text box, so that when you copy and paste your data also you can find the length

  $("input").change(function(){
    //your function of finding the length goes here
  });

If you post the exact problem it will be easier to get help.

Hope this helps :D

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100205

do you mean:

else if (len >= 71 && len <= 133) {
     $("#lbl").text("two SMS");
     if (confirm("more than two sms are you sure?")) {
         $("#lbl").text(len);
     }
}
else if (len >= 134 && len <= 199) {
       $("#lbl").text("three SMS");
       alert("more than three sms are you sure?");
}

Upvotes: 2

Related Questions