In this article I will explain how to use Regular Expression (Regex) to accept/allow only Alphabets and Space in TextBox in ASP.Net using:-
1. RegularExpression Validator.
2. JavaScript.
3. jQuery.
 
1. Regular Expression (Regex) to accept only Alphabets and Space in TextBox using RegularExpression Validator
The following HTML Markup consists of a TextBox, a Button and a RegularExpression Validator to which I have applied the Regular Expression (Regex) to accept only Alphabets and Space as valid characters.
<u>1. Using Regular Expression Validator</u>
<hr />
<asp:TextBox ID="TextBox1" runat="server" />
<br />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
    ValidationExpression="[a-zA-Z ]*$" ErrorMessage="*Valid characters: Alphabets and space." />
<br />
<asp:Button ID="Button1" Text="Validate" runat="server" />
 
Regular Expression (Regex) to accept only Alphabets and Space in TextBox in ASP.Net
 
 
2. Regular Expression (Regex) to accept only Alphabets and Space in TextBox using JavaScript
The following HTML Markup consists of a TextBox, a Button and an HTML SPAN. On the OnClientClick event of the Button, a JavaScript function is executed which validates the TextBox text against the Regular Expression (Regex) and if it contains character other than Alphabets or Space then an error message is displayed.
<u>2. Using Regular Expression and JavaScript</u>
<hr />
<asp:TextBox ID="TextBox2" runat="server" />
<br />
<span id="spnError" style="color: Red; display: none">*Valid characters: Alphabets and
space.</span>
<br />
<asp:Button ID="Button2" Text="Validate" runat="server" CausesValidation="false"
OnClientClick="return Validate()" />
<script type="text/javascript">
function Validate() {
    var isValid = false;
    var regex = /^[a-zA-Z ]*$/;
    isValid = regex.test(document.getElementById("<%=TextBox2.ClientID %>").value);
    document.getElementById("spnError").style.display = !isValid ? "block" : "none";
    return isValid;
}
</script>
 
Regular Expression (Regex) to accept only Alphabets and Space in TextBox in ASP.Net
 
 
3. Regular Expression (Regex) to accept only Alphabets and Space in TextBox using jQuery
The following HTML Markup consists of a TextBox, a Button and an HTML SPAN. To the Button a jQuery click event handler is attached thus when the button is clicked, the TextBox text is validated against the Regular Expression (Regex) and if it contains character other than Alphabets or Space then an error message is displayed.
<u>3. Using Regular Expression and jQuery</u>
<hr />
<asp:TextBox ID="TextBox3" runat="server" />
<br />
<span id="spnError2" style="color: Red; display: none">*Valid characters: Alphabets
and space.</span>
<br />
<asp:Button ID="btnValidate" Text="Validate" runat="server" CausesValidation="false" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
    $("[id*=btnValidate]").bind("click", function () {
        var isValid = false;
        var regex = /^[a-zA-Z ]*$/;
        isValid = regex.test($("[id*=TextBox3]").val());
        $("#spnError2").css("display", !isValid ? "block" : "none");
        return isValid;
    });
});
</script>
 
Regular Expression (Regex) to accept only Alphabets and Space in TextBox in ASP.Net
 
 
Demo
 
Downloads