繁体   English   中英

在UpdatePanel内部的Gridview中进行文本框控件验证c#ASP.Net

[英]Text Box Control Validation In Gridview inside UpdatePanel c# ASP.Net

我有一个gridview ,其中一列包含文本框控件

我想验证用户输入的文本为字母数字和仅空格

允许-> azAZ09空格

我想使用Javascript进行验证

平台ASP.Net 2.0,C#

到目前为止我一直在尝试...

< script type =“ text / javascript ”>

  function IsValidCharNum(event) {
      var KeyBoardCode = (event.which) ? event.which : event.keyCode;
      if ((KeyBoardCode < 96 || KeyBoardCode > 123) && (KeyBoardCode < 65 || KeyBoardCode > 90) && (KeyBoardCode < 48 || KeyBoardCode > 57) && (KeyBoardCode < 32 || KeyBoardCode > 32)) {
          return false;
      }
      return true;
  } </script>

文本框的onkeypress =“ return IsValidCharNum(event)”(没有gridview和更新面板)正在运行

您可以像这样使用RegularExpression Validator

<asp:TextBox ID="txtName" runat="server" ></asp:TextBox>

<asp:RegularExpressionValidator ID="REValphaOnly" runat="server" ErrorMessage="Please enter only alphanumeric." ControlToValidate="txtName" ValidationExpression="^[a-zA-Z0-9 ]+$"></asp:RegularExpressionValidator>

更多信息 :

http://msdn.microsoft.com/en-us/library/ms972966.aspx

http://www.codeproject.com/Tips/472728/RegularExpressionValidator-In-ASP-NET

您可以通过在文本框的keyup上调用javascript来实现。

<script type="text/javascript">
        function ValidateText(i) {
            if (/[^0-9a-bA-B\s]/gi.test(fieldname.value)) {
                alert("Only alphanumeric characters and spaces are valid in this field");
                fieldname.value = "";
                fieldname.focus();
                return false;
            }
        }
    </script>


<asp:TemplateField >
      <ItemTemplate>
              <asp:TextBox ID="TextBox1" runat="server" onkeyup ="ValidateText(this);"></asp:TextBox>
     </ItemTemplate>

</asp:TemplateField>  

最后创建了一个正确的函数来完成验证

page.aspx

<script type="text/javascript">
  function IsValidCharNum(event) {

      var KeyBoardCode = (event.which) ? event.which : event.keyCode;

      if ((KeyBoardCode < 96 || KeyBoardCode > 123) && (KeyBoardCode < 65 || KeyBoardCode > 90) && (KeyBoardCode < 48 || KeyBoardCode > 57) && (KeyBoardCode < 32 || KeyBoardCode > 32)) {

          return false;

      }

      return true;

  }

<asp:TextBox ID="TextBox1" runat="server" onkeypress="return IsValidCharNum(event)"></asp:TextBox>

都使用gridview和updatepanel

暂无
暂无

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

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