How to restrict special characters in textbox using regular expression in asp.net c#

In this article, we learn how to restrict special characters in textbox using regular expression validation in asp.net c#. We just need to put below validator expression in our regular expression validation.

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=" Restrict for special characters" ID="rfvname"
            ValidationExpression="^[\sa-zA-Z0-9]*$">

        </asp:RegularExpressionValidator>


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

Out-put:-



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

1 comment:

Powered by Blogger.