How to allow some special characters using regular expression in asp.net c#

As we learn in our last article how to restrict special character using regular expression in asp.net c#. But many times we need to allow some special characters suppose we are validating address so we do not need to allow “@,#$%” type special characters. Let’s see we just need to put below validation expression in our regular validation expression in asp.net c#

ValidationExpression="^[\sa-zA-Z0-9,@&()-+]*$"


Source code:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="specialtextbox.aspx.cs" Inherits="specialtextbox" %>



<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
       
    <asp:TextBox runat="server" ID="txtname" />
        <asp:RegularExpressionValidator runat="server" ControlToValidate="txtname"
            ForeColor="Red" SetFocusOnError="true" Display="Dynamic"
            ErrorMessage="allow onlysome special characters like ,@&()-+" ID="rfvname"
            ValidationExpression="^[\sa-zA-Z0-9,@&()-+]*$">

        </asp:RegularExpressionValidator>


    </form>
</body>
</html>


Output:-


How to allow some special characters using regular expression in asp.net c# How to allow some special characters using regular expression in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 9:15:00 PM Rating: 5

1 comment:

Powered by Blogger.