【问题标题】:Need Client Side Validation for TextBox if '<' and '>' are used in textbox ASP.NET如果在文本框 ASP.NET 中使用“<”和“>”,则需要对文本框进行客户端验证
【发布时间】:2016-05-31 22:53:51
【问题描述】:

能否请您为 ASP.NET 中的 TextBox 建议一个客户端验证。需要验证并且不应允许用户使用“”。在“”之间输入文本时,应显示验证

【问题讨论】:

  • 我认为您需要验证输入的任何 html 标签,然后是这种情况,请查看stackoverflow.com/questions/3790681/…
  • 这里,我需要阻止用户输入''。如果用户输入这个,那么他们应该得到一个验证消息@ShaminderSAujla

标签: javascript asp.net validation textbox client-side-validation


【解决方案1】:

你可以使用CustomValidator来做到这一点

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator runat="server" id="customvalidator1"   ControlToValidate="TextBox1" 
EnableClientScript="true" ErrorMessage="Cannot accept- < or >" ClientValidationFunction="jsvalidate" />

下面的脚本应该放在aspx中

<script type="text/javascript">
        function jsvalidate(sender, args) {
            debugger;
            var val = document.getElementById('<%=TextBox1.ClientID%>').value;
            if ((val.indexOf('<') !== -1) || (val.indexOf('>') !== -1)) {
                alert("Error");
                args.IsValid = false;
            }
            else
             {
              args.IsValid = true;
              }
        }
    </script>

【讨论】:

  • 这真的很有帮助。它工作正常 :) 如果您以如下方式更新脚本将会很有帮助:如果在 '' 之间存在任何文本,那么只有应该显示验证。否则不需要验证@balaji
  • 无法理解,请详细说明更新
猜你喜欢
  • 2015-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-28
  • 1970-01-01
  • 2014-12-10
相关资源
最近更新 更多