简体   繁体   English

RegularExpressionValidator在asp.net中不起作用

[英]RegularExpressionValidator does not work in asp.net

I want to set MaxLength property of a textbox with multiline in asp.net(for example 100 character). 我想在asp.net中使用多行设置文本框的MaxLength属性(例如100个字符)。 I do not want to use javascript solution. 我不想使用JavaScript解决方案。 I have used : 我用过 :

<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
    <title></title>
  </head>
  <body>
    <form id="form1" runat="server">
    <div>                        
      <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" SetFocusOnError="true" ValidationExpression="^[a-zA-Z.]{0,100}$"></asp:RegularExpressionValidator>
      <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="109px" Width="274px"  ></asp:TextBox>
    </div>
   </form>
 </body>
</html>

and this : 和这个 :

<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
    <title></title>
  </head>
  <body>
   <form id="form1" runat="server">
    <div>                        
      <asp:RegularExpressionValidator ID="rgConclusionValidator2"                   ControlToValidate="TextBox1" ValidationExpression="^[\s\S]{0,100}$" runat="server" SetFocusOnError="true" /></asp:RegularExpressionValidator>
      <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="109px" Width="274px"  ></asp:TextBox>
    </div>
   </form>
 </body>
</html>

but none of them works. 但它们都不起作用。 i have test on latest ie, firefox , chrome , opera. 我测试了最新的ie,firefox,chrome,opera。 thanks alot. 非常感谢。

Use this JavaScript function for maxlength and call it on OnKeypress. 将此JavaScript函数用于maxlength并在OnKeypress上调用它。

function checktextarea(textvalue,message,limit)
{
    var str="";
    var str=textvalue.value;
    if((str.length > limit ))
    {
        alert('Error : '+message + limit + ' characters\n'+ '          You entered text with Length '+str.length);
        textvalue.focus()
        return false;
    }
    else
    {
        return true;
    }

}

It is working, but: 工作,但是:

  1. Validators won't validate on the fly - so it will allow you to enter anything, including rather long texts. 验证器不会即时验证 - 因此它允许您输入任何内容,包括相当长的文本。 It is not like maxlength property which just won't allow you to enter more than 100 characters. 它与maxlength属性不同,它不允许您输入超过100个字符。
  2. You need some action to start validation. 您需要一些操作才能开始验证。 In most cases it is button, so just add it: 在大多数情况下它是按钮,所以只需添加它:

    <asp:Button runat="server" Text="OK" />

  3. You need error message, which will be shown to user if validation fail - add ErrorMessage property to validator: 您需要错误消息,如果验证失败将显示给用户 - 将ErrorMessage属性添加到验证器:

    <asp:RegularExpressionValidator ... ErrorMessage="ERROR!" />

And that's it - type text, click button, see error. 就是这样 - 键入文本,单击按钮,查看错误。

If you do want to restrict text length on the fly - then you need JavaScript, you can see this question for samples Specifying maxlength for multiline textbox 如果你想要动态限制文本长度 - 那么你需要JavaScript,你可以看到这个问题的样本指定多行文本框的maxlength

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM