如何限制文本框中的值 [英] How Do I Limit Values In A Text Box

查看:91
本文介绍了如何限制文本框中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我目前在我的数据库中有一个字段允许输入3000.



在前面最后我有一个用户输入数据的文本框。



我想在用户输入数据时将字符数量限制为每行55个



例如



这是一些文字(55 Chara)line1

这是一些文字(55 Chara)line2

这是一些文字(55 Chara)line3

这是一些文字(20 Chara)line4



以上所有内容应位于同一文本框中

Hi
I currently have a field in my database which allows 3000 to be entered.

On the front end i have a textbox in which users enter the data.

I want to limit the amount of charaters to 55 per line when users are entering the data

e.g.

this is some text(55 Chara)line1
this is some text(55 Chara)line2
this is some text(55 Chara)line3
this is some text(20 Chara)line4

All of the above should be in the same textbox

推荐答案

您可能需要包装文本。检查这个



如何在网页上的文本框中包装文本 [ ^ ]
You probably need to wrap your text. Check this

How to wrap text in a textBox on a web page[^]


您可以使用 Javascript 功能来实现此目的。我已经创建了一个虚拟javascript函数来实现这一目标。请在下面找到相同的内容。



You can use Javascript function to achieve this. I have created one dummy javascript function for achieving this. Please find below same.

// Javascript function to move the text to next line in textbox if it exceeded the limit of maximum charcaters in one line i.e. maxCharInLine 

 <script type="text/javascript">
          function lineLimit(maxCharInLine) {
              var txtDemo = document.getElementById("<%=txtDemo.ClientID%>");
              if (txtDemo.value.lastIndexOf('\n') != -1 || txtDemo.value.length >= maxCharInLine)
              {
                  var trailingText = txtDemo.value.substr(txtDemo.value.lastIndexOf('\n') + 1);
                  if (trailingText.length >= maxCharInLine)
                  {
                      txtDemo.value = txtDemo.value + '\n';
                  }
              }
        }
    </script>

// In declaration of textbox, you have to call above function "lineLimit" on key press and also define the maximum limit of character for one line. Here I have defined 5 as maximum character limit. 

  <asp:TextBox ID="txtDemo" runat="server" TextMode="MultiLine" onkeypress="javascript:lineLimit(5);"></asp:TextBox>





我希望这能帮到你:) :)



享受编码..:)



I hope this will help you :) :)

Enjoy Coding.. :)


Aspx页面:

Aspx page:
<asp:Label ID="lblMessage" runat="server" ForeColor="Red" EnableViewState="false" />
    <p>
        <asp:TextBox ID="txtDetails" runat="server" TextMode="MultiLine" Columns="50" Rows="5" /></p>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="SubmitData" />



cs file:


cs file:

protected void SubmitData(object sender, EventArgs e)
    {
        // allow only 500 characters
        var maxLength = 50;
        if (txtDetails.Text.Trim().Length > maxLength)
        {
            lblMessage.Text = string.Format("Sorry, only {0} characters are allowed.",
            maxLength);
            return;
        }
        // go ahead and write code to save the data
    }



希望这能帮到你


hope this help you


这篇关于如何限制文本框中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆