Need Client Side Validation for TextBox if '<' and '>' are used in textbox ASP.NET

瘦欲@ 提交于 2019-12-12 01:52:57

问题


Could you please Suggest a client Side Validation for the TextBox in ASP.NET. Needs validation and should not allow user to use '<>'. When text is entered between '<' and '>', it should show validation


回答1:


You can do this using 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" />

below script should be placed in 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>


来源:https://stackoverflow.com/questions/35504083/need-client-side-validation-for-textbox-if-and-are-used-in-textbox-asp-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!