【问题标题】:validate length of textarea验证文本区域的长度
【发布时间】:2012-10-29 06:24:02
【问题描述】:

嗨,我正在使用 SMS 面板,我想检查文本区域的长度,现在我正在使用这些代码:

        $("#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?");
            }
        });

我希望当我将文本粘贴到 textArea 时会显示警报并选择 no cant input anything

感谢您的建议?

【问题讨论】:

  • 第一个建议 - 提出可回答的问题。 “不完美”根本不是解释。
  • 看看这个textarea lenght count
  • @zerkms 例如,当我将文本复制到 textarea 时它不起作用。
  • @MilindAnantwar 我不想使用插件
  • 我不同意你的代码不应该在这种情况下工作......

标签: jquery


【解决方案1】:

你的意思是:

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?");
}

【讨论】:

  • 这不是问题。当您达到一定数量的字符时 - 会出现警报。
  • 不,我必须检查一条短信的长度,例如第一条短信的长度为 70,第二条为 63,另一条为 66
【解决方案2】:

但它并不完美,

我可以看到的一个错误是,它不能完美地工作,你已经使用 keydown 事件来查找长度,所以如果你复制和粘贴没有发生 Keydown 事件,所以你找不到长度在那里,因此您可以在文本框的 onchange 事件中编写相同的函数,以便在复制和粘贴数据时也可以找到长度

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

如果您发布确切的问题,将更容易获得帮助。

希望这会有所帮助:D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-03
    • 2011-07-04
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多