How to use Regular Expression (Regex) to accept only Alphanumeric (Alphabets and Numbers) in TextBox in ASP.Net using RegularExpression Validator

Category > ASP.NET || Published on : Wednesday, January 3, 2018 || Views: 8892 || Regular Expression (Regex) accept only Alphanumeric (Alphabets and Numbers) in TextBox RegularExpression Validator


Introduction

Here Pawan Kumar will explain how to use Regular Expression (Regex) to accept only Alphanumeric (Alphabets and Numbers) in TextBox in ASP.Net using RegularExpression Validator

Description

In previous post I have explained How to Call JavaScript function when CheckBoxList is clicked (checked or unchecked) in ASP.NET, jQuery: Call function when CheckBox is checked or unchecked, How to Implement Reorder List (Drag and Drop) using jQuery in ASP.Net, How To Export HTML Table To Excel Using jQuery Plugin, How to Display Decimal Numbers As Money using Transact-SQL, How to Access Data From Ordered Dictionary using C#, and many more articles.

Now I will explain How to How to use Regular Expression (Regex) to accept only Alphanumeric (Alphabets and Numbers) in TextBox in ASP.Net using RegularExpression Validator

So follow the steps to learn How to use Regular Expression (Regex) to accept only Alphanumeric (Alphabets and Numbers) in TextBox in ASP.Net using RegularExpression Validator

I will guide you step by step procedure which will use Regular Expression (Regex) to accept only Alphanumeric (Alphabets and Numbers) in TextBox  using RegularExpression Validator. Let's get started.

Step 1: Create a new website in Visual Studio.(I'm using Visual Studio 2015)

Step 2: Create a new ASP.NET webpage and name it "regularexpression.aspx".

Step 3: Add aspx code as mentioned below.


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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>www.sourcecodehub.com</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <asp:TextBox ID="TextBox1" runat="server" />
            <br />
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ForeColor="Red"
                ValidationExpression="[a-zA-Z0-9]*$" ErrorMessage="*Valid characters: Alphabets and Numbers." />
            <br />
            <asp:Button ID="Button1" Text="Validate" runat="server" />

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

In above code we are using HTML Markup consists of a TextBox, a Button and a RegularExpression Validator which has been applied with the Regular Expression (Regex) to accept only Alphanumeric characters i.e. Alphabets (Upper and Lower case) and Numbers (Digits) as valid characters.

Hope this will help you in application development. Enjoy!!!

 

Conclusion:

So, In this tutorial we have learned, How to use Regular Expression (Regex) to accept only Alphanumeric (Alphabets and Numbers) in TextBox in ASP.Net using RegularExpression Validator